2013-03-14 41 views
0

我正在尝试使用XML来检查客户输入的各个字段的Struts2验证。我的struts.xml延伸struts-default,我有一个非常简单的行动类TestAction其中延伸ActionSupport,但它不起作用。Struts2验证使用XML不起作用?

如果有人能帮助我看看我缺少的东西,我会非常感激。

这是我有:

CustomerAction-validation.xml中

<?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"> 
<validators> 
    <field name="customerName"> 
     <field-validator type="requiredstring"> 
      <message>Required</message> 
     </field-validator> 
    </field> 
</validators> 

struts.xml的

<action name="addCustomer" class="com.yell.hibu.action.CustomerAction" 
      method="execute"> 
      <interceptor-ref name="validation"/> 
      <param name="excludeMethods"> 
        input,back,cancel,browse 
       </param> 
      <interceptor-ref name="fileUpload"> 
       <param name="maximumSize">2097152</param> 
       <param name="allowedTypes"> 
        image/png,image/gif,image/jpeg,image/pjpeg 
       </param> 
      </interceptor-ref> 
      <interceptor-ref name="defaultStack"></interceptor-ref> 
      <result name="success">/success.jsp</result> 
      <result name="input">/registration.jsp</result> 
     </action> 

这里我有Registration.jsp只有1场

<s:form action="addCustomer" id="register-form" method="post" validate="true" theme="xhtml" enctype="multipart/form-data"> 
<s:actionerror/> 
<s:fielderror/> 
<s:textfield name="customer.customerName" label="Customer Name:" cssClass="tooltip" title="Max 10 characters allowed." maxlength="10"/> 

+0

没有足够的信息来帮忙。验证文件在哪里部署?发生了什么/没有发生?动作是否有适当的getter/setter? – 2013-03-14 12:38:16

+0

类TestAction的代码是什么? – 2013-03-14 12:43:13

+0

试试这个链接,希望它能解决你的问题http://www.roseindia.net/struts/struts/struts2.2.1/ValidationInterceptor.html – 2013-03-14 12:56:18

回答

1

<interceptor-ref name="validation"/> 

是自我封闭,那么

<param name="excludeMethods"> 
     input,back,cancel,browse 
</param> 

将永远不会被读取。

Validation Interceptor应后Params Interceptor运行,如图中examples from the documentation

同样,根据文档,

这个拦截往往是在一个堆栈施加的最后(或倒数第二)拦截器中的一个,因为它假定所有的值已经设置在动作。

然后,尝试这样做:

<action name="addCustomer" class="com.yell.hibu.action.CustomerAction" 
     method="execute"> 

    <interceptor-ref name="defaultStack"></interceptor-ref> 

    <interceptor-ref name="validation"> 
     <param name="excludeMethods"> 
      input,back,cancel,browse 
     </param> 
    </interceptor-ref> 

    <interceptor-ref name="fileUpload"> 
     <param name="maximumSize">2097152</param> 
      <param name="allowedTypes"> 
       image/png,image/gif,image/jpeg,image/pjpeg 
      </param> 
    </interceptor-ref> 

    <result name="success">/success.jsp</result> 
    <result name="input">/registration.jsp</result> 
</action> 

如果它不工作,发布您的JSP和行动了。

希望可以帮到