我正在尝试使用XML来检查Struts2验证,以检查Customer输入的各个字段。我的struts.xml
扩展了struts-default
,我有一个非常简单的动作类TestAction
,它扩展了ActionSupport
,但它不起作用。
如果有人能够帮助我看到我缺乏的东西,我将非常感激。
这就是我所拥有的:
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>
这里我只有1个字段的Registration.jsp
<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 :(得分: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和Action。
希望有所帮助