struts2验证validation小结

要验证一个ACTION中的几个方法,可以在不验证的方法前面加上:@SkipValidation
能验证的type有:
<?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">

<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>


方式是:
<field name="username">
	<field-validator type="requiredstring">
		<message>用户名不能为空!</message>
	</field-validator>
</field>
<field name="xh">
        <field-validator type="required">  
            <message key="序号不能为空" />
        </field-validator>
	    <field-validator type="int">  
            <message key="int" />  
        </field-validator>  
        <field-validator type="conversion"> 
            <message>"序号"必须为整数!</message>  
        </field-validator>
    </field>

要和类型转换分开可以修改struts-default.xml的拦截器,也可以在struts.xml中增加一个拦截器屏蔽默认的拦截器
<package name="struts2" extends="struts-default">
	  <interceptors>   
            <interceptor-stack name="myinterceptor">   
                <interceptor-ref name="exception" />   
                <interceptor-ref name="alias" />   
                <interceptor-ref name="servletConfig" />   
                <interceptor-ref name="prepare" />   
                <interceptor-ref name="i18n" />   
                <interceptor-ref name="chain" />   
                <interceptor-ref name="debugging" />   
                <interceptor-ref name="profiling" />   
                <interceptor-ref name="scopedModelDriven" />   
                <interceptor-ref name="modelDriven" />   
                <interceptor-ref name="fileUpload" />   
                <interceptor-ref name="checkbox" />   
                <interceptor-ref name="staticParams" />   
                <interceptor-ref name="params">   
                    <param name="excludeParams">dojo\..*</param>   
                </interceptor-ref>   
                   
                <interceptor-ref name="validation">   
                    <param name="excludeMethods">   
                        input,back,cancel,browse   
                    </param>   
                </interceptor-ref>   
                <interceptor-ref name="workflow">   
                    <param name="excludeMethods">   
                        input,back,cancel,browse   
                    </param>   
                </interceptor-ref>   
            </interceptor-stack>   
        </interceptors>   
	  <default-interceptor-ref name="myinterceptor"></default-interceptor-ref>


资料:
http://www.iteye.com/topic/95620

你可能感兴趣的:(java,xml,struts,dojo)