.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.
|