Platinum Partner
struts

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.

<%@taglib  uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>
Highlight Error Fields
</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<html:form action="login" >
    <table>
        <tr>
            <td>
                User Name
            </td>
            <td>
                <html:text property="userName" errorStyleClass="error"
                errorKey="org.apache.struts.action.ERROR" />
            </td>
            <td>
                <html:errors property="userName" />
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                <html:password property="password" errorStyleClass="error"
                errorKey="org.apache.struts.action.ERROR" />
            </td>
            <td>
                <html:errors property="password" />
            </td>
        </tr>
        <tr>
            <td>
                 <html:submit value="Login" />
            </td>
        </tr>
    </table>
</html:form>
</body>
</html>

The style.css file has a style class "error", which sets the background to light blue color.

.error {
	background-color: #b9ecfd;
}

LoginForm class extends org.apache.struts.validator.ValidatorForm. All the validations are done inside the validate method.

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

    ActionErrors errors = new ActionErrors();
    if (getUserName() == null || getUserName().length() < 1) {
    	errors.add("userName", new ActionMessage("error.userName.required"));
    }
    if (getPassword() == null || getPassword().length() < 1) {
    	errors.add("password", new ActionMessage("error.password.required"));
    } else if (getPassword().length() < 6) {
    	errors.add("password", new ActionMessage("error.password.minlength"));
    }
    return errors;

}

The corresponding error messages are configured in the ApplicationResouce.properties file.

error.userName.required = User Name is required.
error.password.required = Password is required.
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.

Enter only the user name and click the Login button, the following error message will be displayed.

Enter a User Name and a Password less than six characters and click the Login button, the following error message will be displayed.


You can download the source code of the highlighting error fields example by clicking on the Download link below.

Source :Download
Source + Lib :Download

 

 

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}