struts2输入校验小结

strut2对输入校验小结:
1、首先执行类型转换。
2、执行校验框架转换
3、执行特定方法对应的validate验证(test,validateTest)
4、执行validate()方法

如果以上所有过程出现了任何错误,都不会继续执行,页面将转向struts.xml中input这个result对应的页面。

以下内容为struts2中xwork对输入校验的解析。

定义校验文件,校验文件命名为对应的Actionname-validate.xml。
    内容可如下进行校验:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC 
"-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
"http://www.opensymphony.com/xwork/xwork-validator1.0.2.dtd"> 
<validators> 
<field name="username"> 
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username invalid </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="age">
<field-validator type="required">
<param name="trim">true</param>
<message>age required </message>
</field-validator>
<field-validator type="int">
<param name="max">150</param>
<param name="min">1</param>
<message>age should be between ${min} and ${max} </message>
</field-validator> 
</field>

<field name="birthday">
<field-validator type="required">
<message>birthday requried</message>
</field-validator>
<field-validator type="date">
<param name="min">2000-01-01</param>
<param name="max">2003-12-31</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field> 
</validators>


    代码说明:
为什么type类型会是required等呢?
这是因为type类型定义在xwork包里有个default.xml
内容如下:
<?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-config-1.0.dtd">
<!-- START SNIPPET: validators-default -->
<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>
<!--  END SNIPPET: validators-default -->
通过阅读default.xml源码我们还可以进一步去查看后面映射的方法,了解其中的参数应用,因为这一切都不是凭空想象!

你可能感兴趣的:(struts2)