參考項(xiàng)目 前臺(tái)頁面 <body> <s:actionerror/> 用戶注冊 <s:form action="/registerAction" method="post"> <s:textfield name="user.userName" label="用戶名"></s:textfield> <s:textfield name="user.name" label="真實(shí)姓名"></s:textfield> <s:textfield name="user.age" label="年齡"></s:textfield> <s:textfield name="user.email" label="郵件"></s:textfield> <s:textfield name="user.homePage" label="主頁"></s:textfield> <s:submit value="注冊"></s:submit> </s:form> </body> Action層 import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport{ private static final long serialVersionUID = 1L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { } } 驗(yàn)證 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts./dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="user.userName"> <field-validator type="requiredstring"> <message>請輸入用戶名</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>用戶名必須在${minLength}和${maxLength}之間</message> </field-validator> </field> <field name="user.name"> <field-validator type="requiredstring"> <message>請輸入姓名</message> </field-validator> </field> <field name="user.age"> <field-validator type="int"> <param name="min">18</param> <message>年齡必須滿18周歲</message> </field-validator> </field> <field name="user.email"> <field-validator type="requiredstring"> <message>請輸入郵件</message> </field-validator> <field-validator type="email"> <message>郵件格式不對</message> </field-validator> </field> <field name="user.homePage"> <field-validator type="requiredstring"> <message>請輸入主頁</message> </field-validator> <field-validator type="url"> <message>主頁格式不對</message> </field-validator> </field> <validator type="expression"> <param name="expression"><![CDATA[!user.name.equals(user.userName)]]></param> <message>用戶名和真實(shí)姓名不能相同</message> </validator> </validators> 自定義驗(yàn)證 整體目錄 validators.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator Config 1.0//EN" "http://struts./dtds/xwork-validator-config-1.0.dtd"> <validators> <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> <validator name="sensitive" class="com.java1234.validators.SensitiveWordValidators"/> </validators> Action層 import com.opensymphony.xwork2.ActionSupport; public class ValidationAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String execute() throws Exception { } } action-validation.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts./dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <message>請輸入姓名</message> </field-validator> <field-validator type="sensitive"> <message>有敏感詞匯</message> </field-validator> </field> </validators> 自定義驗(yàn)證 import com.opensymphony.xwork2.validator.ValidationException; import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; public class SensitiveWordValidators extends FieldValidatorSupport{ @Override public void validate(Object object) throws ValidationException { String fieldName=this.getFieldName(); String value=this.getFieldValue(fieldName, object).toString(); if(!check(value)){ this.addFieldError(fieldName, object); } } public boolean check(String value){ String sensitiveWords[]={"操","草"}; for(int i=0;i<sensitiveWords.length;i++){ if(value.indexOf(sensitiveWords[i])>-1){ return false; } } return true; } } |
|