Search

Highlighting Error Fields in Struts Tutorial

In this example we will see how to highlight error fields. This is done by creating a seperate style to apply when an error has occred. This style value is set to the errorStyleClass attribute of the corresponding Struts html tag. In our example the login.jsp page contains the following code.

01.<%@taglib  uri="/WEB-INF/struts-html.tld" prefix="html" %>
02.<html>
03.<head>
04.<title>
05.Highlight Error Fields
06.</title>
07.<link href="style.css" rel="stylesheet" type="text/css" />
08.</head>
09.<body>
10.<html:form action="login" >
11.<table>
12.<tr>
13.<td>
14.User Name
15.</td>
16.<td>
17.<html:text property="userName" errorStyleClass="error"
18.errorKey="org.apache.struts.action.ERROR" />
19.</td>
20.<td>
21.<html:errors property="userName" />
22.</td>
23.</tr>
24.<tr>
25.<td>
26.Password
27.</td>
28.<td>
29.<html:password property="password" errorStyleClass="error"
30.errorKey="org.apache.struts.action.ERROR" />
31.</td>
32.<td>
33.<html:errors property="password" />
34.</td>
35.</tr>
36.<tr>
37.<td>
38.<html:submit value="Login" />
39.</td>
40.</tr>
41.</table>
42.</html:form>
43.</body>
44.</html>
The style.css file has a style class "error", which sets the background to light blue color.
1..error {
2.background-color#b9ecfd;
3.}
LoginForm class extends org.apache.struts.validator.ValidatorForm. All the validations are done inside the validate method.
01.public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
02. 
03.ActionErrors errors = new ActionErrors();
04.if (getUserName() == null || getUserName().length() < 1) {
05.errors.add("userName"new ActionMessage("error.userName.required"));
06.}
07.if (getPassword() == null || getPassword().length() < 1) {
08.errors.add("password"new ActionMessage("error.password.required"));
09.else if (getPassword().length() < 6) {
10.errors.add("password"newActionMessage("error.password.minlength"));
11.}
12.return errors;
13. 
14.}
The corresponding error messages are configured in the ApplicationResouce.properties file.
1.error.userName.required = User Name is required.
2.error.password.required = Password is required.
3.error.password.minlength = Password can not be less than 6 characters.
Run the application. The login.jsp page will be displayed. Click the login button without entering the User Name and the Passowrd. The following error messages will be displayed to the user.

Struts DynaActionForm Tutorial

DynaActionForm Beans are the extension of Form Beans that allows you to specify the form properties inside the struts configuration file instead of creating a seperate concreate class. It will become tedious to create a seperate form bean for each action class. Using DynaActionForm we can easily create Form Bean in struts-config.xml file. The struts-config.xml file entry for the DyanActionForm Bean is shown below.

1.<form-beans>
2.<form-bean name="LoginForm"type="org.apache.struts.action.DynaActionForm">
3.<form-property name="userName" type="java.lang.String" />
4.<form-property name="password" type="java.lang.String" />
5.</form-bean>
6.</form-beans>
The type attribute points to org.apache.struts.action.DynaActionForm and the <form-property> tag is used to define all the form variables. The <form-property> tag has the following three attributes.
·         name - The unique name of the property.
·         initial - The default value of the property.
·         type - Defines the Java type of the property. The available types are
o    java.math.BigDecimal
o    java.math.BigInteger
o    boolean and java.lang.Boolean
o    byte and java.lang.Byte
o    char and java.lang.Character
o    java.lang.Class
o    double and java.lang.Double
o    float and java.lang.Float
o    int and java.lang.Integer
o    long and java.lang.Long
o    short and java.lang.Short
o    java.lang.String
o    java.sql.Date
o    java.sql.Time
o    java.sql.Timestamp
Now we will see how to access the DyanActionForm in the action class.
01.public class LoginAction extends org.apache.struts.action.Action {
02. 
03.public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throwsException {
04.DynaActionForm loginForm = (DynaActionForm) form;
05.String userName = loginForm.get("userName").toString();
06.String password = loginForm.get("password").toString();
07.if(userName.equals(password))
08.{
09.return mapping.findForward("success");
10.}
11.else
12.{
13.return mapping.findForward("failure");
14.}
15.}
16.}
We need to typecast the form object to DynaActionForm object in the execute method of the action class. After that we can access the Form Bean properties. We will consider a simple login application for our example. Here we will check the user name and password, if they are equal then we will forward the user to the success page, else we will forward the user to the failue page.