Custom Validators in Struts 2.0
Introduction
Struts 2.0 is the popular Open Source Presentation Tier framework developed by Apache Group. It is based on MVC Model 2 design pattern. Dispatcher Filter is the front controller for the struts2 based applications. Struts 2.0 has simplified web development for its users by introducing POJO based actions, interceptors, flexible validation and support for many different result types. Struts can be used to build the user interface tier of the enterprise application. Data validation is an important aspect in enterprise applications. This article explains the concept of custom validators.
Development Environment
- NetBeans IDE 6.9
- GlassFish V3
Project Structure
The sample application developed in this article is ‘Struts2CustomValidation’
where a user can register to the application by specifying few details like userId,firstname,city etc. Custom validator developed in the application will be responsible for validating the value of the city field.
Libraries/Jar Files Required
- Struts 2.0 jar files
The complete application structure is shown below:
- The User Interface (JSP pages) is created in the “Web Pages” directory. The java classes (Actions, Interceptors and struts.xml) are created in the “Source Packages” directory. The required jar files are present in the “Libraries” directory. The web application deployment descriptor “web.xml” is created by the IDE in the “WEB-INF” subdirectory of “Web Pages”.
Action Class
Struts does the request processing with the help of Action classes. Here’s the code for the RegisterAction.java which handles the
request to register to our application. This class is created in the package “com.actions”.
RegisterAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package com.actions;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class RegisterAction extends ActionSupport {
private String userId;
private String firstName;
private String lastName;
private int age;
private String city;
private String email;
private Date joiningDate;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Date getJoiningDate() {
return joiningDate;
}
public void setJoiningDate(Date joiningDate) {
this.joiningDate = joiningDate;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String execute() {
return "success";
}
} |
Let us understand the request processing logic in the action class. The action class handles the register request in the execute method. There’s no validation logic hardcoded in the java class. The complete validation logic including the custom validation is added in the “RegisterAction-validation.xml” file.Upon successful validation, the user is forwarded to “Register_Success.jsp” page.
We have put the validation logic in the “RegisterAction-validation.xml” file kept in the “com.actions” package. Please note that the validation.xml file should always be kept in the same package where your action class is present.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userId">
<field-validator type="requiredstring">
<message>Employee Id is a mandatory field</message>
</field-validator>
<field-validator type="regex">
<param name="expression">
<![CDATA[([E][m][p][0-9][0-9][0-9])]]>
</param>
<message>Valid Employee Id required e.g. Emp001</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">20</param>
<param name="max">80</param>
<message>Age needs to between ${min} and ${max}</message>
</field-validator>
</field>
<field name="firstName">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>First Name is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">5</param>
<param name="trim">true</param>
<message>
Enter firstName between 5 and 10 characters
</message>
</field-validator>
</field>
<field name="lastName">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Last Name is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">5</param>
<param name="trim">true</param>
<message>
Enter lastName between 5 and 10 characters
</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Password is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">5</param>
<param name="trim">true</param>
<message>
Enter password between 5 and 10 characters
</message>
</field-validator>
</field>
<field name="city">
<field-validator type="requiredstring" short-circuit="true">
<param name="trim">true</param>
<message>You can not leave city as blank</message>
</field-validator>
<field-validator type="cityValidator" short-circuit="true">
<message>City value is incorrect. it must match the pattern Az*</message>
</field-validator>
</field>
<field name="email">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>You can not leave email as blank</message>
</field-validator>
<field-validator type="email">
<message>
The email address you entered is not valid.
</message>
</field-validator>
</field>
<field name="joiningDate">
<field-validator type="required">
<message>
You cannot leave the date of joining field blank.
</message>
</field-validator>
<field-validator type="date" >
<message>
Joining date must be supplied
</message>
</field-validator>
</field>
</validators> |
The data validation is performed with the help of “RegisterAction-validation.xml” file. Different validation routines are configured for the the various attributes.Along with the various built in validations, we have made use of a custom validator “cityValidator” to validate the value of the “city” attribute. The id of the validator “cityValidator” is taken from the file validators.xml which is created in the default class path.







April 1, 2011
Struts 2.0