Validators in Struts2

Validators in Struts2

Here is an example validator for bean class User.

RegisterAction-validation.xml
 1  <? xml version="1.0" encoding="GBK" ?>
 2  <! DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
 3   "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
 4  < validators >
 5       < field  name ="name" >
 6           < field-validator  type ="requiredstring"  short-circuit ="true" >
 7               < message > Name is null! </ message >
 8           </ field-validator >
 9          
10           < field-validator  type ="regex" >
11               <!--  必须匹配正则表达式:只能是字母和数字,且长度必须在4到25之间  -->
12               < param  name ="expression" > <![CDATA[ (\w{4,25}) ]]> </ param >
13               < message > Your name must only have english characters and digits, length from 4 to 25. </ message >
14           </ field-validator >
15       </ field >
16      
17      
18       < field  name ="password" >
19           < field-validator  type ="requiredstring"  short-circuit ="true" >
20               < param  name ="trim" > true </ param >
21               < message > Password is missing! </ message >
22           </ field-validator >
23           < field-validator  type ="regex" >
24               < param  name ="expression" > <![CDATA[ (\w{4,25}) ]]> </ param >
25               < message > Password can only have english characters, length from 4 to 25. </ message >
26           </ field-validator >
27       </ field >
28      
29       < field  name ="age" >
30           < field-validator  type ="int" >   <!-- 必须在指定范围内 -->
31               < param  name ="min" > 1 </ param >
32               < param  name ="max" > 150 </ param >
33               < message > Age can only between 1 and 150. </ message >
34           </ field-validator >
35       </ field >
36  </ validators >

field name="name" "password" and "age" are 3 variables in RegisterAction.java. They can be like this way:
 1  package  com.googlesites.qslbinghamton.xmlexamples;
 2 
 3  import  com.opensymphony.xwork2.ActionSupport;
 4 
 5  public   class  RegisterAction  extends  ActionSupport {
 6       private  String name;
 7       private  String password;
 8       private   int  age;
 9       public  String getName() {
10           return  name;
11      }
12       public   void  setName(String name) {
13           this .name  =  name;
14      }
15       public  String getPassword() {
16           return  password;
17      }
18       public   void  setPassword(String password) {
19           this .password  =  password;
20      }
21       public   int  getAge() {
22           return  age;
23      }
24       public   void  setAge( int  age) {
25           this .age  =  age;
26      }
27      @Override
28       public  String execute()  throws  Exception {
29           /**
30           * Save user by creating user object
31           * 
32           * User user = new User();
33           * user.setName(name);
34           * user.setPassword(password);
35           * user.setAge(age);
36           * 
37           * userService.save(user);
38           *   */
39           return  SUCCESS;
40      }
41  }

Other than this, struts2 have OO oriented, thus ***-validation.xml can be changed such as in field name="user.username"

RegisterAction-validation.xml
 1  <? xml version="1.0" encoding="GBK" ?>
 2  <! DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
 3   "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
 4  < validators >
 5       < field  name ="user.name" >
 6           < field-validator  type ="requiredstring"  short-circuit ="true" >
 7               < message > Name is null! </ message >
 8           </ field-validator >
 9          
10           < field-validator  type ="regex" >
11               <!--  必须匹配正则表达式:只能是字母和数字,且长度必须在4到25之间  -->
12               < param  name ="expression" > <![CDATA[ (\w{4,25}) ]]> </ param >
13               < message > Your name must only have english characters and digits, length from 4 to 25. </ message >
14           </ field-validator >
15       </ field >
16      
17      
18       < field  name ="user.password" >
19           < field-validator  type ="requiredstring"  short-circuit ="true" >
20               < param  name ="trim" > true </ param >
21               < message > Password is missing! </ message >
22           </ field-validator >
23           < field-validator  type ="regex" >
24               < param  name ="expression" > <![CDATA[ (\w{4,25}) ]]> </ param >
25               < message > Password can only have english characters, length from 4 to 25. </ message >
26           </ field-validator >
27       </ field >
28      
29       < field  name ="user.age" >
30           < field-validator  type ="int" >   <!-- 必须在指定范围内 -->
31               < param  name ="min" > 1 </ param >
32               < param  name ="max" > 150 </ param >
33               < message > Age can only between 1 and 150. </ message >
34           </ field-validator >
35       </ field >
36  </ validators >

RegisterAction.java can only have User user as a member variable without String name, String password, int age separatedly.
The advantage is quite clear.
 1  package  com.googlesites.qslbinghamton.xmlexamples;
 2 
 3  import  com.opensymphony.xwork2.ActionSupport;
 4 
 5  public   class  RegisterAction  extends  ActionSupport {
 6       private  User user;
 7      
 8       public  User getUser() {
 9           return  user;
10      }
11 
12       public   void  setUser(User user) {
13           this .user  =  user;
14      }
15 
16      @Override
17       public  String execute()  throws  Exception {
18           /**
19           * Save user by creating user object:
20           * 
21           * userService.save(user);
22           *   */
23           return  SUCCESS;
24      }
25  }
26 


Also, take a loot at class User, as xxxAction-validation.xml can show that User has name, password and age as its attributes.
 1  public   class  User {
 2       private   long  id;
 3       private  String name;
 4       private  String password;
 5       private   int  age;
 6       public   long  getId() {
 7           return  id;
 8      }
 9       public   void  setId( long  id) {
10           this .id  =  id;
11      }
12       public  String getName() {
13           return  name;
14      }
15       public   void  setName(String name) {
16           this .name  =  name;
17      }
18       public  String getPassword() {
19           return  password;
20      }
21       public   void  setPassword(String password) {
22           this .password  =  password;
23      }
24       public   int  getAge() {
25           return  age;
26      }
27       public   void  setAge( int  age) {
28           this .age  =  age;
29      }
30  }

Attribute id will be created automatically when insert user into database, therefore, there's no need to create input or validator for id.


Now, let me review all common validator's type and their configuration one by one:

1. Must have a input validator

1  <!--  Must have input  -->
2       < validators >
3           < field  name ="username" >
4               < field-validator  type ="required" >
5                   < message > must have input </ message >
6               </ field-validator >
7           </ field >
8       </ validators >

2. Must hava a String input validator
1  <!--  Must have String input  -->
2       < validators >
3           < field  name ="username" >
4               < field-validator  type ="requiredstring" >
5                   < param  name ="trim" > true </ param >   <!--  trim the space before and after string input  -->
6                   < message > Must have string input </ message >
7               </ field-validator >
8           </ field >
9       </ validators >

3. Integer/Float validator

 1  <!--  valid integer or float check  -->
 2       < validators >
 3           < field  name ="age" >
 4               < field-validator  type ="int" >
 5                   < param  name ="min" > 1 </ param >
 6                   < param  name ="max" > 150 </ param >
 7                   < message > Age must be in range from 1 to 150 </ message >
 8               </ field-validator >
 9           </ field >
10       </ validators >

4. Date validator
 1  <!--  valid date check  -->
 2       < validators >
 3           < field  name ="birth" >
 4               < field-validator  type ="date" >
 5                   < param  name ="min" > 1900-01-01 </ param >
 6                   < param  name ="max" > 2050-02-21 </ param >
 7                   < message  key ="birth.range"   />
 8               </ field-validator >
 9           </ field >
10       </ validators >

5. Regular expression validator
 1  <!--  valid regular expression check  -->
 2       < validators >
 3           < field  name ="username" >
 4               < field-validator  type ="regex" >
 5                   <!--  Must match regular expression: english character, 4 to 25  -->
 6                   < param  name ="expression" > <![CDATA[ (\w{4,25}) ]]> </ param >
 7                   < message > Your name must only have english characters and digits, length from 4 to 25. </ message >
 8               </ field-validator >
 9           </ field >
10       </ validtors >

6. Password validator

 1  <!--  valid integer or float check  -->
 2       < validators >
 3           < field  name ="age" >
 4               < field-validator  type ="int" >
 5                   < param  name ="min" > 1 </ param >
 6                   < param  name ="max" > 150 </ param >
 7                   < message > Age must be in range from 1 to 150 </ message >
 8               </ field-validator >
 9           </ field >
10       </ validators >

Notice: must have both String password and String repassword in ***Action.java

7. Email address validator
1  <!--  valid email  -->
2       < validators >
3           < field  name ="email" >
4               < field-validator  type ="email" >
5                   < message > Must be a valid email address </ message >
6               </ field-validator >
7           </ field >
8       </ validators >

8. URL address validator
1  <!--  valid url  -->
2       < validators >
3           < field  name ="url" >
4               < field-validator  type ="url" >
5                   < message > Must be a valid url address </ message >
6               </ field-validator >
7           </ field >
8       </ validators >

9. String length validator
 1  <!--  String length check  -->
 2       < validators >
 3           < field  name ="username" >
 4               < field-validator  type ="stringlength" >
 5                   < param  name ="minlength" > 4 </ param >
 6                   < param  name ="maxlength" > 20 </ param >
 7                   < message > username must between length 4 and 20 characters </ message >
 8               </ field-validator >
 9           </ field >
10       </ validators >

10. type="visitor" validator

When there's only User user object in ***.java which extends ActionSupport.

***-validation.xml can be: (*** is the name as ***.java)

RegisterAction-validation.xml
 1  <!--  Vistor  -->
 2       < validators >
 3           < field  name ="user" >
 4               < field-validator  type ="visitor" >
 5                   < param  name ="context" > userContext </ param >
 6                   < param  name ="appendPrefix" > true </ param >
 7                   < message > Input error: </ message >
 8               </ field-validator >
 9           </ field >
10       </ validators >

Also create User-userContext-validation.xml under the same package with User bean:

***-*****-validation.xml's naming rule: *** must be the bean's name such as User;
***** must be the value from <param name="context">*****</param> in ****-validation.xml

User-userContext-validation.xml
 1  <? xml version="1.0" encoding="UTF-8" ?>
 2 
 3  <! DOCTYPE validators PUBLIC
 4      "-//OpenSymphony Group//XWork Validator 1.0//EN"
 5      "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
 6  < validators >
 7       < field  name ="name" >
 8           < field-validator  type ="requiredstring" >
 9               < param  name ="trim" > true </ param >
10               < message > 必须输入名字 </ message >
11           </ field-validator >
12           < field-validator  type ="regex" >
13               < param  name ="expression" > <![CDATA[ (\w{4,25}) ]]> </ param >
14               < message > 您输入的用户名只能是字母和数组,且长度必须在4到25之间 </ message >
15           </ field-validator >
16       </ field >
17       < field  name ="password" >
18           < field-validator  type ="requiredstring" >
19               < param  name ="trim" > true </ param >
20               < message > 必须输入密码 </ message >
21           </ field-validator >
22           < field-validator  type ="regex" >
23               < param  name ="expression" > <![CDATA[ (\w{4,25}) ]]> </ param >
24               < message > 您输入的密码只能是字母和数组,且长度必须在4到25之间 </ message >
25           </ field-validator >
26       </ field >
27       < field  name ="age" >
28           < field-validator  type ="int" >
29               < param  name ="min" > 1 </ param >
30               < param  name ="max" > 150 </ param >
31               < message > 年纪必须在1到150之间 </ message >
32           </ field-validator >
33       </ field >
34       < field  name ="birth" >
35           < field-validator  type ="date" >
36               < param  name ="min" > 1900-01-01 </ param >
37               < param  name ="max" > 2050-02-21 </ param >
38               < message > 年纪必须在${min}到${max}之间 </ message >
39           </ field-validator >
40       </ field >
41  </ validators >


你可能感兴趣的:(Validators in Struts2)