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.
<!-- Standard Action 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.

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

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

        <message-resources parameter="ApplicationResources"/>

    Here we declared one action named submitForm. SubmitForm defines path /submit so whenever server request for / 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.
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 */
            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.
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 / 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.
<form action="/submit" method="post">
    Last name: <input type="text" name="lastName">
    address:<br/><textarea name="address" rows="2" cols="30"> address</textarea>

    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.