DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Highlighting Error Fields in Struts Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (1)
Save
Tweet
42.82K Views

Join the DZone community and get the full member experience.

Join For Free

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

 

 

Error message Download application Light (web browser) Links Attribute (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • How to Make Git Forget a Tracked File Now in .gitignore
  • How to Build Security for Your SaaS User Communications
  • How Java Apps Litter Beyond the Heap

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo