Backward to Struts1

Is it really sucks if you are forced to use Struts 1 after you have been using Struts 2 for ages? Maybe yes, maybe no, at least it is not that difficult to adopt it in a short time even they are quite different.


This article will show you a simple Struts 1 example. You could follow the link to see check how you would write a similar application with Struts 2.!8D4E97688DE1E193!113.entry


Struts HTML Tags      

A complete reference of Struts HTML tags could be found at


<%@ taglib prefix="html" uri="/WEB-INF/tld/struts-html.tld" %>

<html:html locale="true">          

<html:form action="/whitepaper" method="post">





             <td><html:text property="title" size="30" maxlength="30"/></td>

             <td><span style="color: red"><html:errors property="errTitle"/></span></td>




              <td><html:submit value="Submit"/></td>




    <html:hidden property="articleID" value="${param.articleID}"/>





Once you got the HTML form, you need to write bean class to associate all the fields with bean properties. Beside you may also need to have your validation method within your bean.


public ActionErrors validate(

        ActionMapping mapping, HttpServletRequest request) {

    ActionErrors errors = new ActionErrors();


    if (getTitle() == null || getTitle().length() < 1) {

        errors.add("errTitle", new ActionMessage("*Field required.<br/>", false));


    return errors;


If any fields failed the validation you can indicate with a message respectively. The message could be hardcode within your new ActionMessage object or can be referred from your message bundle



In your struts configuration file you need to specify the mappings between your action classes and forms. If you have a message bundle, you should also declare it here.


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" ""&gt;



        <form-bean name="loginForm" type=""/>



<forward name="help" path="/ "/>



        <action path="/whitepaper"








    <message-resources parameter=""/>



Of course you also need to declare your configuration file inside the web.xml file.












Action Class

Your class extends from Action class will be the place where you decide what operation you would to run and where you would like the page to forward to.


public class WhitepaperAction extends Action {  

    public ActionForward execute(ActionMapping mapping,

                                 ActionForm form,

                                 HttpServletRequest request,

                                 HttpServletResponse response) throws Exception {


        String filePath = getServlet().getServletContext().getInitParameter("PATH");

        WhitepaperForm whitepaperForm = (WhitepaperForm) form;

        ActionForward forward = new RedirectingActionForward();



        return forward;



You have the options to decide whether using forward or redirect. Forward is a default, but redirect would be very helpful if you want to go to parent directory.

This entry was posted in Struts. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s