Struts2驗證框架是基於Struts攔截器開發的,具備良好的擴展性;通常的驗證均可以支持。如今咱們以一個註冊驗證的例子進行總結:java
新建一個model,User:express
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 @Override 38 public String toString() { 39 return "User [userName=" + userName + ", name=" + name + ", age=" + age 40 + ", email=" + email + ", homePage=" + homePage + "]"; 41 } 42 }
寫RegisterAction.java類:apache
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 @Override 11 public String execute() throws Exception { 12 System.out.println("傳入的用戶:"+user); 13 return SUCCESS; 14 } 15 }
寫一下RegisterAction的驗證框架:框架
RegisterAction-validation.xml validation.xml文件名要注意和Action名同樣!jsp
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 3 <validators> 4 <field name="user.userName"> 5 <field-validator type="requiredstring"> 6 <message>請輸入用戶名</message> 7 </field-validator> 8 <field-validator type="stringlength"> 9 <param name="minLength">6</param> 10 <param name="maxLength">10</param> 11 <message>用戶名必須在${minLength}和${maxLength}之間</message> 12 </field-validator> 13 </field> 14 <field name="user.name"> 15 <field-validator type="requiredstring"> 16 <message>請輸入姓名</message> 17 </field-validator> 18 </field> 19 <field name="user.age"> 20 <field-validator type="int"> 21 <param name="min">18</param> 22 <message>年齡必須滿18週歲</message> 23 </field-validator> 24 </field> 25 <field name="user.email"> 26 <field-validator type="requiredstring"> 27 <message>請輸入郵件</message> 28 </field-validator> 29 <field-validator type="email"> 30 <message>郵件格式不對</message> 31 </field-validator> 32 </field> 33 <field name="user.homePage"> 34 <field-validator type="requiredstring"> 35 <message>請輸入主頁</message> 36 </field-validator> 37 <field-validator type="url"> 38 <message>主頁格式不對</message> 39 </field-validator> 40 </field> 41 //驗證表達式 42 <validator type="expression"> 43 <param name="expression"><![CDATA[!user.name.equal(user.userName)]]></param> 44 <message>用戶名和真實姓名不能相同!</message> 45 </validator> 46 </validators>
在struts.xml中配置:ide
1 <struts> 2 <package name="manager" extends="struts-default"> 3 <action name="registerAction" class="com.java1234.action.RegisterAction"> 4 <result name="input">/register.jsp</result> 5 <result name="success">/success.jsp</result> 6 </action> 7 </package> 8 </struts>
註冊頁面register.jsp:post
1 <body> 2 <s:actionerror/> //能夠將驗證表達式的值輸出; 3 用戶註冊 4 <s:form action="/registerAction" method="post"> 5 <s:textfield name="user.userName" label="用戶名"></s:textfield> 6 <s:textfield name="user.name" label="真實姓名"></s:textfield> 7 <s:textfield name="user.age" label="年齡"></s:textfield> 8 <s:textfield name="user.email" label="郵件"></s:textfield> 9 <s:textfield name="user.homePage" label="主頁"></s:textfield> 10 <s:submit value="註冊"></s:submit> 11 </s:form> 12 </body>
success.jsp頁面:ui
1 <body> 2 註冊成功 3 </body>
開始驗證:this
其中struts的默認的Validators:url
1 <validators> 2 <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> 3 <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> 4 <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> 5 <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> 6 <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> 7 <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> 8 <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> 9 <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> 10 <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> 11 <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> 12 <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> 13 <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> 14 <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> 15 <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> 16 </validators>
如今咱們自建立檢查敏感詞彙的驗證器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 @Override 11 public String execute() throws Exception { 12 System.out.println("姓名:"+name); 13 return SUCCESS; 14 } 15 }
ValidationAction-validation.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 3 <validators> 4 <field name="name"> 5 <field-validator type="requiredstring"> 6 <message>請輸入姓名</message> 7 </field-validator> 8 <field-validator type="sensitive"> 9 <message>有敏感詞彙!</message> 10 </field-validator> 11 </field> 12 </validators>
Validators配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 3 <validators> 4 <field name="name"> 5 <field-validator type="requiredstring"> 6 <message>請輸入姓名</message> 7 </field-validator> 8 <field-validator type="sensitive"> 9 <message>有敏感詞彙!</message> 10 </field-validator> 11 </field> 12 </validators>
設置驗證器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<sensitive.length;i++){ 15 if(value.indexOf(sensitive[i])>-1){ 16 return false; 17 } 18 } 19 return true; 20 } 21 }
在validators.xml中加入:
1 <validator name="sensitive" class="com.java1234.Validators.SensitiveWordValidators"/>
配置struts.xml:
1 <struts> 2 <package name="manager" extends="struts-default"> 3 <action name="validationAction" class="com.java1234.action.ValidationAction"> 4 <result name="input">/validation.jsp</result> 5 <result name="success">/success.jsp</result> 6 </action> 7 </package> 8 </struts>
建立validation.jsp文件:
1 自定義驗證 2 <s:form action="/validationAction" method="post"> 3 <s:textfield name="name" label="姓名"></s:textfield> 4 <s:submit value="提交"></s:submit> 5 </s:form>
驗證結果: