一 文件名约定:
1) 验证XML的文件名: Action类名-validation.xml , 并与相关的Action类放在一起.
2) 当一个Action中有多个业务方法时 : Action 类 名-映射名-validation.xml
什么是映射名:<action name="login".../> ,name属性的值:login就是映射名, 千万注意,很多人写错
例子:
URL是:http://localhost/struts/user_addUser. action
struts.xml是:
<action name="user_*" class="ssh.org.web.UserAction" method="{1}"> <result name="success">/user/success.jsp</result> <result name="input">/user/user.jsp</result> </action>
匹配后<action name="user_*".../> 实为<action name="user_addUser" .../>
所以验证框架XML文件名是: UserAction-user_addUser -validation.xml
二 验证不通过时, 会执行addFieldError ()方法, 在JSP页面, 要用 <s:fielderror />显示信息.
关于addFieldError,请看第6讲:6 addActionError与addFieldError
三 例子
Registration-validation.xml : 用于校验Registration.java类
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <!-- 非字段校验器 <validator type="requiredstring"> <param name="fieldName">username</param> <message>username should not be blank!</message> </validator> <validator type="stringlength"> <param name="fieldName">username</param> <param name="minLength">6</param> <param name="maxLength">10</param> <message>username should be between ${minLength} and ${maxLength}</message> </validator> --> 字段校验器--> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>username should not be blank!</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>username should be between ${minLength} and ${maxLength}</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>password should not be blank!</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>password should be between ${minLength} and ${maxLength}</message> </field-validator> </field> <field name="repassword"> <field-validator type="requiredstring"> <message>repassword should not be blank!</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>repassword should be between ${minLength} and ${maxLength}</message> </field-validator> </field> <field name="age"> <field-validator type="required"> <message>age should not be blank!</message> </field-validator> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>age should be between ${min} and ${max}</message> </field-validator> </field> <field name="birthday"> <field-validator type="required"> <message>birthday should not be blank!</message> </field-validator> <field-validator type="date"> <param name="min">2001-01-01</param> <param name="max">2003-12-31</param> <message>birthday should be between ${min} and ${max}</message> </field-validator> </field> <field name="graduation"> <field-validator type="required"> <message>graduation should not be blank!</message> </field-validator> <field-validator type="date"> <param name="min">2005-01-01</param> <param name="max">2007-12-31</param> <message>graduation should be between ${min} and ${max}</message> </field-validator> </field> </validators>
四 Struts2内建的 校验器有:
<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="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/> <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> <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"/> </validators>