Struts2验证框架是基于Struts拦截器开发的,具有良好的扩展性;一般的验证都可以支持。现在我们以一个注册验证的例子进行总结:新建一个model,User:1publiccl Struts2验证框架是基于Struts拦
Struts2验证框架是基于Struts拦截器开发的,具有良好的扩展性;一般的验证都可以支持。现在我们以一个注册验证的例子进行总结:
新建一个model,User:
1 public class User { 2 private String userName; 3 private String name; 4 private int age; 5 private String email; 6 private String homePage; 7 public String getUserName() { 8 return userName; 9 }10 public void setUserName(String userName) {11 this.userName = userName;12 }13 public String getName() {14 return name;15 }16 public void setName(String name) {17 this.name = name;18 }19 public int getAge() {20 return age;21 }22 public void setAge(int age) {23 this.age = age;24 }25 public String getEmail() {26 return email;27 }28 public void setEmail(String email) {29 this.email = email;30 }31 public String getHomePage() {32 return homePage;33 }34 public void setHomePage(String homePage) {35 this.homePage = homePage;36 }37 @Override38 public String toString() {39 return "User [userName=" + userName + ", name=" + name + ", age=" + age40 + ", email=" + email + ", homePage=" + homePage + "]";41 }42 }写RegisterAction.java类:
1 public class RegisterAction extends ActionSupport{ 2 private static final long serialVersiOnUID= 1L; 3 private User user; 4 public User getUser() { 5 return user; 6 } 7 public void setUser(User user) { 8 this.user = user; 9 }10 @Override11 public String execute() throws Exception {12 System.out.println("传入的用户:"+user);13 return SUCCESS;14 }15 }写一下RegisterAction的验证框架:
RegisterAction-validation.xml validation.xml文件名要注意和Action名一样!
1 2 DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 3 4 5 6 请输入用户名 7 8 9 610 1011 用户名必须在${minLength}和${maxLength}之间12 13 14 15 16 请输入姓名17 18 19 20 21 1822 年龄必须满18周岁23 24 25 26 27 请输入邮件28 29 30 邮件格式不对31 32 33 34 35 请输入主页36 37 38 主页格式不对39 40 41 //验证表达式42 43 !user.name.equal(user.userName)]]>44 用户名和真实姓名不能相同!45 46 View Code在struts.xml中配置:
1 2 3 4 /register.jsp5 /success.jsp6 7 8注册页面register.jsp:
1 2 //可以将验证表达式的值输出; 3 用户注册 4 5 6 7 8 9 10 11 12success.jsp页面:
1 2 注册成功3开始验证:
其中struts的默认的Validators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16现在我们自创建检查敏感词汇的验证器ValidationAction:
1 public class ValidationAction extends ActionSupport{ 2 private static final long serialVersiOnUID= 1L; 3 private String name; 4 public String getName() { 5 return name; 6 } 7 public void setName(String name) { 8 this.name = name; 9 }10 @Override11 public String execute() throws Exception {12 System.out.println("姓名:"+name);13 return SUCCESS;14 }15 }ValidationAction-validation.xml:
1 2 DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 3 4 5 6 请输入姓名 7 8 9 有敏感词汇!10 11 12Validators配置:
1 2 DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 3 4 5 6 请输入姓名 7 8 9 有敏感词汇!10 11 12设置验证器SensitiveWordValidators:
1 public class SensitiveWordValidators extends FieldValidatorSupport{ 2 @Override 3 public void validate(Object object) throws ValidationException { 4 // TODO Auto-generated method stub 5 //获取输入的用户字段 6 String fieldName=this.getFieldName(); 7 String value=this.getFieldValue(fieldName, object).toString(); 8 if(!check(value)){ 9 this.addFieldError(fieldName, object);10 }11 }12 public boolean check(String value){13 String sensitive[]={"王八","tmd","md"};14 for(int i=0;i-1){16 return false;17 }18 }19 return true;20 }21 }在validators.xml中加入:
1配置struts.xml:
1 2 3 4 /validation.jsp5 /success.jsp6 7 8创建validation.jsp文件:
1 自定义验证2 3 4 5验证结果: