【JavaEE企业应用学习记录】验证配置

 1 package sanglp;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator;
 5 import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
 6 import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
 7 import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
 8 
 9 import java.util.Date;
10 
11 /**
12  * Created by Administrator on 2016/10/11.
13  */
14 public class RegistAction  extends ActionSupport{
15     private String name;
16     private String pass;
17     private int age;
18     private Date birth;
19 
20     public String getName() {
21         return name;
22     }
23 
24     public String getPass() {
25         return pass;
26     }
27 
28     public int getAge() {
29         return age;
30     }
31 
32     public Date getBirth() {
33         return birth;
34     }
35 
36     @RequiredStringValidator(key = "name.required",message = "用户名必须填写")
37     @RegexFieldValidator(regexExpression = "\w{4,25}",key = "name.regex" ,message = "用户名必须是4到25位数字或字母")
38     public void setName(String name) {
39         this.name = name;
40     }
41 
42     @RequiredStringValidator(key = "pass.required",message = "密码不能为空")
43     @RegexFieldValidator(regexExpression = "\w{4,25}",key = "pass.regex",message = "密码必须是4到25位的数字或字母")
44     public void setPass(String pass) {
45         this.pass = pass;
46     }
47 
48     @IntRangeFieldValidator(message = "",key = "age.range" ,max = "150",min = "1")
49     public void setAge(int age) {
50         this.age = age;
51     }
52 
53     @DateRangeFieldValidator(message = "",key = "birth.range",min="1900/01/01",max = "2050/01/21")
54     public void setBirth(Date birth) {
55         this.birth = birth;
56     }
57     public  void validate(){
58         System.out.println("进去validate方法进行校验"+name);
59         if(!name.contains("crazyit")){
60             addFieldError("user","您的用户名必须包含crazyit");
61         }
62     }
63 }

为了在input视图对应的JSP页面输出错误提示,应该在页面增加

<s:fielderror/>

原文地址:https://www.cnblogs.com/dream-to-pku/p/5950773.html