May 25, 2009

Struts – Action – Example

    In my earlier post (Struts - What & Why?) I have explained the basic architecture of struts base application. Now I gonna explain you how to create application through struts.

    There is a predefined structure to implement struts in our web application.

(1)    First of all create web.xml. Declare a servlet as below.
e.g.
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>application</param-name>
        <param-value>ApplicationResources</param-value>
    </init-param>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

    We are not declaring any controller servlet here. As we are going to use struts. In struts controller servlet are being declared in struts-comfig.xml.

(2)    Create one file struts-config.xml. place this file in WEB-INF directory.
e.g.
<struts-config>

    <!-- Form Bean Definitions -->
    <form-beans>
        <form-bean name="submitForm" type="com.forms.SubmitForm"/>
    </form-beans>

    <!-- Action Mapping Definitions -->
    <action-mappings>
        <action path="/submit" type="com.action.SubmitAction" name="submitForm">
            <forward name="success" path="/index.jsp"/>
            <forward name="failure" path="/submit.jsp"/>
        </action>

    </action-mappings>
        <message-resources parameter="ApplicationResources"/>
    </struts-config>

    Here we declared one action named submitForm. SubmitForm defines path /submit so whenever server request for /submit.do submitForm action will be called.

    forward tag defines that where to forward request after its processing. Here we define submit.jsp for failure. So if request been failed to process then server will forward the request to submit.jsp. As well for success we define index.jsp.

(3)    Now define a class SubmitAction extending org.apache.struts.action.Action class.
As in servlet, we defines a method as doPost or doGet to process. Here define a method execute as in example.
e.g.
public class SubmitAction extends Action {
    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){

        /* here we gets object of ActionMapping, ActionForm, HttpServletRequest and HttpResponse.*/

        SubmitForm submitForm = (SubmitForm)form;

        /* if lastName field left blank then procees will be stoped and control will go to submit form again */
        if(submitForm.getLastName()==null)
            return mapping.findForward("failure");

        String lastName = submitForm.getLastName();
        request.setAttribute("lastName", lastName.toUpperCase());

        return mapping.findForward("success");
    }
}

mapping : this object is used to forward request after processing as in example.
form : explain later in this post.

(4)    Now just look at the struts-config.xml, there is tag defined by form-beans. This tag defines the form class. The form class is a simple javabean which contains the fields, we are going to pass through the form from jsp or html. Fields' name must match with the names of the parameter of the request.
e.g.
public class SubmitForm extends ActionForm {
    private String lastName = "";
    private String address = "";

    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

    Here parameter passed through the /submit.do request must be lastName and address.

    Whenever one submit the form from submit.jsp all the fields of form class will be set with the values passed in the request. So the object we are getting in action class as argument have all the field filled by the user.

(5)    Now for the testing create one jsp named submit.jsp.
e.g.
<form action="/submit" method="post">
    Last name: <input type="text" name="lastName">
    address:<br/><textarea name="address" rows="2" cols="30"> address</textarea>
</form>

    This is how the whole structure of a struts action implemented. This is a very basic functionality. This may be extend to a large application where more one controller are defined as well as forms.

No comments: