首页 > 代码库 > Struts2校验器
Struts2校验器
一、Struts2内建校验器
位于xwork-2.0.4.jar压缩包中( com.opensymphony.xwork2.validator.validators)有个文件default.xml ,该文件中定义了Struts2框架内建的校验器。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>
二、各种类型校验器
1、类型转换检验器:
(1)非字段校验:
<validator type="conversion">
<param name="fieldName">myField</param>
<message>类型转换错误</message>
<param name ="repopulateField">true</param>
</validator>
(2)字段校验:
<field name="myField">
<field-validator type="conversion">
<message>类型转换错误</message>
<param name ="repopulateField">true</param>
</field-validator>
</field>
- fieldName:该参数指定检查是否存在转换异常的字段名称,如果是字段校验,则不用指定该参数。
- repopulateField:该参数指定当类型转换失败后,返回input页面时,类型转换失败的表单是否保留原来的错误输入。true为保留,false为不保留。
2、日期校验器:
(1)非字段校验:
<validator type="date">
<param name="fieldName">birthday</param>
<param name="min">1990-01-02</param>
<param name="max">2010-07-28</param>
<message>生日数据错误</message>
</validator>
(2)字段校验:
<field name="birthday">
<field-validator type="date">
<param name="min">1990-01-01</param>
<param name="max">2010-07-28</param>
<message key="error.birthday"></message>
</field-validator>
</field>
- min:指定字段日期值的最小值,该参数为可选参数。
- max:指定字段日期值的最大值,该参数为可选参数。
3、浮点数值校验器:
(1)非字段校验:
<validator type="double">
<param name="fieldName">percentage</param>
<param name="minInclusive">20.1</param>
<param name="maxInclusive">50.1</param>
<message>生日数据错误</message>
</validator>
(2)字段校验:
<field name="percentage">
<field-validator type="double">
<param name="minInclusive">20.1</param>
<param name="maxInclusive">50.1</param>
<message key="error.percentage"></message>
</field-validator>
</field>
- minInclusive|minExclusive:指定字段的最小值,包含该值|不包含该值。
- maxInclusive|maxExclusive:指定字段的最大值, 包含该值|不包含该值。
4、邮件地址校验器:
(1)非字段校验:
<validator type="email">
<param name="fieldName">MyEmail</param>
<message>非法的邮件地址</message>
</validator>
(2)字段校验:
<field name="MyEmail">
<field-validator type="email">
<message>非法的邮件地址</message>
</field-validator>
</field>
5、表达式校验器:
<validator type="expression">
<param name="expression">.......</param>
<message>Failed to meet Ognl Expression...</message>
</validator>
- expression:该参数为一个逻辑表达式,该参数使用OGNL表达式,并基于值栈计算,返回一个Boolean类型值。
6、字段表达式校验器:
(1)非字段校验:
<validator type="fieldexpression">
<param name="fieldName">myField</param>
<param name="expression"><![CDATA[#myCreditLimit > #myGirfriendCreditLimit]]></param>
<message>My credit limit should be MORE than my girlfriend</message>
</validator>
(2)字段校验:
<field name="myField">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[#myCreditLimit > #myGirfriendCreditLimit]]></param>
<message>My credit limit should be MORE than my girlfriend</message>
</field-validator>
</field>
7、整数校验器:
(1)非字段校验:
<validator type="int">
<param name="fieldName">age</param>
<param name="min">10</param>
<param name="max">100</param>
<message>年龄必须在在${min}到${max}之间</message>
</validator>
(2)字段校验:
<field name="age">
<field-validator type="int">
<param name="min">10</param>
<param name="max">100</param>
<message>年龄必须在在${min}到${max}之间</message>
</field-validator>
</field>
8、正则表达式校验器:
(1)非字段校验:
<validator type="regex">
<param name="fieldName">myStrangePostcode</param>
<param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]></param>
</validator>
(2)字段校验:
<field name="myStrangePostcode">
<field-validator type="regex">
<param name="expression"><![CDATA[#myCreditLimit > #myGirfriendCreditLimit]]></param>
<message>My credit limit should be MORE than my girlfriend</message>
</field-validator>
</field>
- expression:为必选参数,指定匹配有的表达式。
- caseSensitive:指明进行匹配时,是否区分大小写,为可选参数,默认为true。
9、必填校验器:
(1)非字段校验:
<validator type="required">
<param name="fieldName">username</param>
<message>用户名不能为空</message>
</validator>
(2)字段校验:
<field name="username">
<field-validator type="required">
<message>用户名不能为空</message>
</field-validator>
</field>
10、必填字符串校验器:
(1)非字段校验:
<validator type="requiredstring">
<param name="fieldName">username</param>
<param name="trim">true</param>
<message>用户名不能为空</message>
</validator>
(2)字段校验:
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空</message>
</field-validator>
</field>
- trim:可选参数,用于指定是否在校验之前对字符串进行整理,默许为true。
11、字符串长度校验器:
(1)非字段校验:
<validator type="stringlength">
<param name="fieldName">username</param>
<param name="minLength">4</param>
<param name="maxLength">10</param>
<message>用户名长度在${minLength}到${maxLength}之间</message>
</validator>
(2)字段校验:
<field name="username">
<field-validator type="stringlength">
<param name="minLength">4</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message key="error.length.username"></message>
</field-validator>
</field>
12、网址校验器:
(1)非字段校验:
<validator type="url">
<param name="fieldName">myHomePage</param>
<message>Invalid homepage url</message>
</validator>
(2)字段校验:
<field name="myHomePage">
<field-validator type="url">
<message>Invalid homepage url</message>
</field-validator>
</field>
13、visitor校验器:
该校验器名称为:visitor,用来校验Action中定义的复合类型属性,支持简单的复合类型、数组类型、Map等集合类型。
(1)非字段校验:
<validator type="visitor">
<param name="fieldName">user</param>
<param name="context">myContext</param>
<param name="appendPrefix">true</param>
</validator>
(2)字段校验:
<field name="user">
<field-validator type="visitor">
<param name="context">myContext</param>
<param name="appendPrefix">true</param>
</field-validator>
</field>
如果想自定义一个自己的校验器,可以在Web工程的WEB-LIB\lib目录下增加一个validators.xml配置文件,在该文件中定义自己的校验器。
注意:如果使用validators.xml配置文件,则系统默认的default.xml配置文件不会加载,所以需要将default.xml的内容复制到validators.xml配置文件中。否则不能使用系统内建的校验器。
Struts2校验器