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.

http://mybiblebook.spaces.live.com/blog/cns!8D4E97688DE1E193!113.entry

 

Struts HTML Tags      

A complete reference of Struts HTML tags could be found at

http://struts.apache.org/1.2.x/userGuide/struts-html.html

 

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

<html:html locale="true">          

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

    <table>

       <tbody>

          <tr>

             <td>*Title:</td>

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

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

          </tr>

           <tr>

              <td></td>

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

           </tr>

       </tbody>

    </table>

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

 </html:form>

</html:html>

 

Validation

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

 

Mapping

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" "http://struts.apache.org/dtds/struts-config_1_2.dtd"&gt;

<struts-config>

    <form-beans>

        <form-bean name="loginForm" type="com.informa.ipi.bt.struts.form.LoginForm"/>

</form-beans>

<global-forwards>

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

    </global-forwards>

<action-mappings>

        <action path="/whitepaper"

                name="whitepaperForm"

                validate="true"

                scope="request"

                input="/whitepaper.jsp"

                type="com.informa.ipi.bt.struts.action.WhitepaperAction">           

        </action>

    </action-mappings>

    <message-resources parameter="com.informa.ipi.bt.ApplicationResources"/>

</struts-config>

 

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

 

    <servlet>

        <servlet-name>action</servlet-name>

        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

        <init-param>

            <param-name>config</param-name>

            <param-value>/WEB-INF/struts-config.xml</param-value>

        </init-param>

        <load-on-startup>0</load-on-startup>

</servlet>

 

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();

 

        forward.setPath(".."+resourcePath+resourceFileBean.getFileUrl());

        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.

Advertisements
This entry was posted in Struts. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s