struts2_04_简单校验和校验框架

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>

 

 

常用验证规则

 

 

验证名称

含义

参数

required

必填

 

requiredstring

必填字符串

trim

stringlength

字符串长度

trim

minLength

maxLength

int

数字范围

min

max

date

时间范围

min

max

regex

正则验证

expression

email

电子邮件格式验证

 

url

路径格式验证

 

fieldexpression

表达式验证

expression

 

 

验证步骤

1.    创建验证文件,可拷贝xwork-core-2.2.1.1.jar/com/opensymphony/xwork2/validator/validators.default.xml

   

验证Action的所有方法,文件名为Action名字-validation.xml

验证Action的某个方法,文件名为Action名字-方法路径-validation.xml

文件要和Action类放在同一目录

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE validators PUBLIC

        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"

        "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">

<validators>

</validators>

 

2.    使用field标签验证字段

 

字符串必填验证

    <field name="uname">

       <field-validator type="requiredstring">

           <message>用户名必填</message>

       </field-validator>

</field>

 

字符串长度验证

<field name="pwd">

       <field-validator type="stringlength">

           <param name="minLength">6</param>

           <param name="maxLength">20</param>

           <message>密码必须为6-20位字符</message>

       </field-validator>

</field>

 

Email格式验证

    <field name="email">

       <field-validator type="email">

           <message>电子邮箱格式不正确</message>

       </field-validator>

</field>

 

正则验证

    <field name="photoFileName">

       <field-validator type="regex">

           <param name="expression">.*\.(png|gif|jpg|jpeg)</param>

           <message>图片格式不正确</message>

       </field-validator>

    </field>

 

 

表达式验证

    <field name="pwd2">

       <field-validator type="fieldexpression">

           <param name="expression">(pwd eq pwd2)</param>

           <message>密码和确认密码必须相等</message>

       </field-validator>

    </field>

你可能感兴趣的:(struts2)