Introduction to the Struts Web Framework 5

 

 

 

 

 

Setting Up an Error Message

 

If the login form is returned, it would be good to inform the user that validation failed. You can accomplish this by adding an error field in the form bean, and an appropriate <bean:write> tag to the form in login.jsp. Finally, in the Action object, set the error message to be displayed in the event that the failure view is chosen.

 

1.      Open LoginForm (LoginForm.java) and add an error field to the class:

 

// error message

private String error;

 

NetBeans with struts framework project - adding error message variable

 

2.      Add a getter method and a setter method for error, as demonstrated above.

 

NetBeans with struts framework project - generating getter and setter methods for error messages

 

NetBeans with struts framework project - selecting the String variable

 

3.      Modify the setter method so that it appears as follows:

 

public void setError() {

    this.error =

        "<span style='color:red'>Please provide valid entries for both fields</span>";

}

 

NetBeans with struts framework project - the getter and setter source code

 

4.      Open login.jsp and make the following changes:

 

<html:form action="/login">

    <table border="0">

        <tbody>

            <tr>

                <td colspan="2">

                    <bean:write name="LoginForm" property="error" filter="false"/>

                    &nbsp;</td>

            </tr>

            <tr>

                <td>Enter your name:</td>

                <td><html:text property="name" /></td>

            </tr>

 

NetBeans with struts framework project - adding source code to action form

 

5.      In LoginAction, within the if conditional clause, add a statement to set the error message before forwarding the failure condition (changes in bold):

 

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 '@'

 

    formBean.setError("Some error");

    return mapping.findForward(FAILURE);

}

 

NetBeans with struts framework project - adding source code to the LoginAction

 

Your completed LoginAction class should now appear as follows:

 

public class LoginAction extends org.apache.struts.action.Action {

    private static final String SUCCESS = "success";

    private static final String FAILURE = "failure";

 

    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 '@'

 

            formBean.setError("Some error");

            return mapping.findForward(FAILURE);

        }

 

        return mapping.findForward(SUCCESS);

 

    }

}

 

 

 

 

 

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

 

NetBeans with struts framework project - the edited loginaction source code

 

Adding forward Entries to struts-config.xml

 

In order for the application to match JSP pages with forwarding conditions returned by LoginAction's execute method, you need to add forward entries to the struts-config.xml file.

 

1.      Open struts-config.xml in the Source Editor, right-click anywhere in the action entry for LoginForm, and choose Struts > Add Forward.

 

NetBeans with struts framework project - adding Forward to the ActionForm

 

2.      In the Add Forward dialog box, type success in Forward Name. Enter the path to success.jsp in the Resource File field (i.e., /WEB-INF/success.jsp). The dialog box should now look as follows:

 

NetBeans with struts framework project - completing the Forward information

 

3.      Click Add. Note that the following forward entry was added to struts-config.xml (changes in bold):

 

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

    <forward name="success.jsp" path="/WEB-INF/success.jsp"/>

</action>

 

 

4.      Perform the same action to add a forward entry for failure. Set the Resource File path to /login.jsp.

 

NetBeans with struts framework project - adding another Forward

 

NetBeans with struts framework project - failure Forward information

 

The following forward entry is added to struts-config.xml (changes in bold):

 

<forward name="success" path="/WEB-INF/success.jsp"/>

<forward name="failure" path="/login.jsp"/>

 

NetBeans with struts framework project - the added Forward entry in the struts-config.xml

 

 

 


 Strut & Web 4 | Back to Main | Strut & Web 6