Struts2自带的校验框架有字段优先和校验器优先两种:
字段优先:1.要写一个xml命名规则,以对应的action的名字开头加上"-validation.properties"。例如action的名字为Register。则xml名字为:
Register-validation.properties。此配置文件与action在同一个package中。
2.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <!--Struts2自带的校验框架方法: 共有两种校验方法 1)field字段优先 2)校验器优先 下面是字段优先 --> <field name="username"> <field-validator type="requiredstring"> <message>用户名不得为空!!</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">10</param> <!-- key的提示信息放在了资源信息中 如果是提交时汉语优先则调用package_zh_CN.properties 如果是英文优先则调用package_en_US.properties配置文件 --> <message key="username.invalid"></message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>密码不得为空!!</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">10</param> <message>密码长度必须在4到6之间!!</message> </field-validator> </field> <field name="age"> <field-validator type="required"> <message>年龄必填!</message> </field-validator> <field-validator type="int"> <param name="min">10</param> <param name="max">50</param> <message>年龄必须在${min}到${max}岁之间!</message> </field-validator> </field> <field name="birthday"> <field-validator type="required"> <message>出生日期必填</message> </field-validator> <field-validator type="date"> <param name="min">2000-01-01</param> <param name="max">2013-01-01</param> <message>出生日期必须在2000-01-01到2013-01-01之间</message> </field-validator> </field> </validators>
3.massage中key属性对应的语言提示配置文件,key的提示信息放在了资源信息中 如果是提交时汉语优先则调用package_zh_CN.properties如果是英文优先则调用package_en_US.properties配置文件。此配置文件与action在同一个package中。
#package_en_US.properties中的内容
username.invalid=Username invalid
package_zh_CN.properties中的内容:
username.invalid=\u7528\u6237\u540D\u4E0D\u5408\u6CD5
4.validator中的type属性值在xwork-core-2.3.4.jar中的com.opensymphony.xwork2.validator.validators包的
default.xml中可以找到
<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>