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;
2. Add a getter method and a setter method for error, as demonstrated above.
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>";
}
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"/>
</td>
</tr>
<tr>
<td>Enter your name:</td>
<td><html:text property="name" /></td>
</tr>
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);
}
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);
}
}
----------------------------------------------
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.
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:
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.
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"/>
Strut & Web 4 | Back to Main | Strut & Web 6