Introduction to the Struts Web Framework 4

 

 

 

 

 

Creating an Action Class

 

The Action class contains the business logic in the application. When form data is received, it is the execute method of an Action object that processes the data and determines which view to forward the processed data to. Because the Action class is integral to the Struts framework, NetBeans IDE provides you with a wizard.

 

1.      In the Projects window, right-click the MyStrutsApp project node and choose New > Other. From the Struts category choose Struts Action and click Next.

 

NetBeans with struts framework project - adding new component into the struts project

 

 

 

 

NetBeans with struts framework project - adding struts Action into the project

 

2.      In the Name and Location panel, change the name to LoginAction.

3.      Select com.myapp.struts in the Package drop-down list.

4.      Type /login in Action Path. This value must match the value you set for the action attribute of the <html:form> tags in login.jsp. Make sure settings appear as in the screenshot below, and then click Next.

 

NetBeans with struts framework project - setting the name and location for new strut action

 

5.      In the third step of the wizard, you are given the opportunity to associate the Action class with a form bean. Notice that the LoginForm bean you previously created is listed as an option for ActionForm Bean Name. Make the following adjustments to the panel:

 

a.       Delete the forward slash for the Input Resource field.

b.      Set Scope to Request (Session is the default scope setting in Struts.)

c.       Deselect the Validate ActionForm Bean option.

 

NetBeans with struts framework project - setting the ActionForm bean parameters

 

Click Finish. The LoginAction class is generated, and the file opens in the Source Editor. Also note that the following action entry is added to the struts-config.xml file:

 

<action-mappings>

    <action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.LoginAction" validate="false"/>

    <action path="/Welcome" forward="/welcomeStruts.jsp"/>

</action-mappings>

 

NetBeans with struts framework project - the action-mapping of the actionForm bean

 

The name and scope attributes apply to the form bean that is associated with the action. Specifically, when an incoming request matches /login, the Struts framework automatically instantiates a LoginForm object and populates it with the form data sent in the request. The default value of validate is set to true. This tells the framework to call the validate method of the form bean. You deselected this option in the wizard however because you will hand-code simple validation in the next step, which does not require the validate method.

 

Implementing Simple Validation

 

In the Source Editor, browse through the LoginAction class (LoginAction.java) and look at the execute method:

 

public ActionForward execute(ActionMapping mapping, ActionForm form,

    HttpServletRequest request, HttpServletResponse response)

    throws Exception {

 

    return mapping.findForward(SUCCESS);

}

 

 

 

 

 

---------------------------------------------------------

 

NetBeans with struts framework project - the execute method

 

Notice the definition of SUCCESS, listed beneath the LoginAction class declaration:

 

private final static String SUCCESS = "success";

 

Currently, the mapping.findForward method is set to unconditionally forward any request to an output view called success. This is not really desirable; you want to first perform some sort of validation on the incoming data to determine whether to send the success view, or any different view.

  1. Accessing Bean Data and Preparing a Forwarding Condition

  2. Setting Up an Error Message

 

Accessing Bean Data and Preparing a Forwarding Condition

 

1.      Type in the following code within the body of the execute method:

 

// extract user data

LoginForm formBean = (LoginForm)form;

String name = formBean.getName();

String email = formBean.getEmail();

 

In order to use the incoming form data, you need to take execute's ActionForm argument and cast it as LoginForm, then apply the getter methods that you created earlier.

 

2.      Type in the following conditional clause to perform validation on the incoming data:

 

// perform validation

if ((name == null) ||             // name parameter does not exist

    email == null  ||             // email parameter does not exist

    name.equals("") ||            // name parameter is empty

    email.indexOf("@") == -1) {   // email lacks '@'

 

    return mapping.findForward(FAILURE);

}

 

 

 

 

At this stage, the execute method should look as follows:

 

public ActionForward execute(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

 

    // extract user data

    LoginForm formBean = (LoginForm) form;

    String name = formBean.getName();

    String email = formBean.getEmail();

 

    // perform validation

    if ((name == null) || // name parameter does not exist

            email == null || // email parameter does not exist

            name.equals("") || // name parameter is empty

            email.indexOf("@") == -1) {   // email lacks '@'

 

        return mapping.findForward(FAILURE);

    }

 

    return mapping.findForward(SUCCESS);

}

 

NetBeans with struts framework project - the edited execute method

 

3.      Add a declaration for FAILURE to the LoginAction class (changes in bold):

 

private static final String SUCCESS = "success";

private static final String FAILURE = "failure";

 

NetBeans with struts framework project -  adding the LoginAction class

 

Using the above logic, the execute method forwards the request to the success view if the user provides an entry for both name and email fields, and the email entered contains an '@' sign. Otherwise, the failure view is forwarded. As will be demonstrated below in Adding forward Entries to struts-config.xml, you can set the failure view to point back to the form page, so that the user has another chance to enter data in the correct format.

 

 

 

 


 Strut & Web 3 | Back to Main | Strut & Web 5