`

WebWork的Validation

    博客分类:
  • java
阅读更多
WebworkWorkflowXML框架Servlet
       WebWork依赖XWork的校验框架,在action执行之前,来启动对action的输入数据的校验规则的运用。
1,添加校验器,在/WEB-INF/class添加一个名为validators.xml,添加自定义校验器
xml 代码
  

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
    "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">

<validators>  
    <validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>  
    <validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>  
    <validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>  
    <validator name="double" class="com.opensymphony.xwork.validator.validators.DoubleRangeFieldValidator" />  
    <validator name="date" class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>  
    <validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>  
    <validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>  
    <validator name="email" class="com.opensymphony.xwork.validator.validators.EmailValidator"/>  
    <validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator"/>  
    <validator name="visitor" class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/>  
    <validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>  
    <validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>  
validators>  
2,打开校验,在xwork定义interceptor,为需要开发校验的action开启拦截器,拦截器的校验范围
xml 代码

<interceptors>  
              
            <interceptor name="adminOnly" class="adminInterceptor"/> 

             <interceptor-stack name="defaultStack">  
                <interceptor-ref name="exception"/>  
                <interceptor-ref name="alias"/>  
                <interceptor-ref name="servlet-config"/>  
                <interceptor-ref name="prepare"/>  
                <interceptor-ref name="i18n"/>  
                <interceptor-ref name="chain"/>  
                <interceptor-ref name="model-driven"/>  
                <interceptor-ref name="fileUpload"/>  
                <interceptor-ref name="static-params"/>  
                <interceptor-ref name="params"/>  
                <interceptor-ref name="conversionError"/>  
                <interceptor-ref name="validation">  
                    <param name="excludeMethods">cancel,execute,delete,edit,list,defaultparam>  
                interceptor-ref>  
                <interceptor-ref name="workflow">  
                    <param name="excludeMethods">input,back,cancelparam>  
                interceptor-ref>  
            interceptor-stack>  
            <interceptor-stack name="validationWorkflowStack">  
                <interceptor-ref name="defaultStack"/>  
                <interceptor-ref name="validation"/>  
                <interceptor-ref name="workflow"/>  
            interceptor-stack>-->  
            <interceptor-stack name="fileUploadStack">  
                <interceptor-ref name="fileUpload"/>  
                <interceptor-ref name="defaultStack"/>  
            interceptor-stack>  
            <interceptor-stack name="adminCheck">  
                <interceptor-ref name="defaultStack"/>  
                <interceptor-ref name="adminOnly"/>  
            interceptor-stack>  
        interceptors>  
3,校验规则,每个action类,指定在ActionName-validation.xml文件中,
xml 代码

<!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="customerCreditDetails.customer.id">  
        <field-validator type="required">  
            <message key="errors.required"/>  
        field-validator>  
        <field-validator type="stringlength">  
            <param name="minLength">1param>  
            <param name="trim">trueparam>  
            <message key="errors.required"/>  
        field-validator>  
   field>  
   <field name="customerCreditDetails.amount">  
        <field-validator type="required">  
            <message key="errors.required"/>  
        field-validator>  
        <field-validator type="double">  
            <param name="minInclusive">1.0param>  
            <message key="errors.intLeast"/>  
        field-validator>  
    field>  
    <field name="customerCreditDetails.operateTime">  
        <field-validator type="required">  
            <message key="errors.required"/>  
        field-validator>  
        <field-validator type="date">  
            <message key="errors.required"/>  
        field-validator>  
    field>  
    <field name="customerCreditDetails.currencyType">  
        <field-validator type="required">  
            <message key="errors.required"/>  
        field-validator>      
        <field-validator type="int">  
            <param name="min">1param>  
            <message key="errors.required"/>  
        field-validator>  
    field>  
    <field name="customerCreditDetails.payType">  
        <field-validator type="required">  
            <message key="errors.required"/>  
        field-validator>      
        <field-validator type="int">  
            <param name="min">1param>  
            <message key="errors.required"/>  
        field-validator>  
    field>  
>  
这是简单验证
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics