首页 > 代码库 > struts-validate.xml配置详解demo

struts-validate.xml配置详解demo

<?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">
<!--此文件必须和UserAction.java在同一个目录下-->  
<!--文件名是Action-validation.xml -->  
<validators>
    <field name="username">
    
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="login.name.null"></message>
        </field-validator>
        
        <field-validator type="stringlength">
            <param name="maxLength">10</param>
            <param name="minLength">2</param>
            <message>用户名长度须在2和10之间</message>
        </field-validator>
    </field>
    
</validators>


<!-- 
required:字段不能为空

requiredstring:字符串不能为空

int:int类型(可指定范围)

long:long类型(可指定范围)

short:short类型(可指定范围)

double:double类型(可指定范围)

date:时间格式(可指定范围)

expression:ognl表达式判断

fieldexpression:ognl表达式判断

email:邮箱判断

url:url路径判断

visitor:把同一个验证程序配置文件用于多个动作(对一个Bean写验证文件,每个使用的Action只要引用)

conversion:格式转换

stringlength:字符串长度

regex:正则表达式判断
<?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>  
    <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 null</message>  
        </field-validator>  
        <field-validator type="stringlength">  
            <param name="minLength">6</param>  
            <param name="maxLength">10</param>  
            <message>password should between ${minLength} and ${maxLength}</message>  
        </field-validator>  
    </field>  
    <field name="repassword">  
        <field-validator type="requiredstring">  
            <message>repassword should not be null</message>  
        </field-validator>  
        <field-validator type="stringlength">  
            <param name="minLength">6</param>  
            <param name="maxLength">10</param>  
            <message>repassword should be between 6 and 10</message>  
        </field-validator>  
    </field>  
    <field name="age">  
        <field-validator type="int">  
            <param name="min">1</param>  
            <param name="max">150</param>  
            <message>age should be between 1 and 150</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">1998-12-12</param>  
            <param name="max">2009-12-12</param>      
            <message>the 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-12-12</param>  
            <param name="max">2007-12-12</param>      
            <message>the graduation should be between ${min} and ${max}</message>  
        </field-validator>  
    </field>  
    
    <field name="user.password">
        <field-validator type="requiredstring">
            <message>密码不能为空</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">4</param>
            <message>密码长度至少${minLength}位</message>
        </field-validator>
    </field>
    <field name="repassword">
        <field-validator type="fieldexpression">
            <param name="expression">user.password==repassword</param>
            <message>密码和确认密码必须相同</message>
        </field-validator>
    </field>
</validators> -->

 

struts-validate.xml配置详解demo