Reputation: 166
I'm trying to use Struts2 validation using XML to check various fields entered by Customer. My struts.xml
extends struts-default
, and I have a extremely simple action class TestAction
which extends ActionSupport
, but it is not working.
If anyone is able to help me see what I lack, I would be extremely grateful.
Here's what I have:
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>
Here I have Registration.jsp for only 1 field
<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"/>
Upvotes: 0
Views: 4639
Reputation: 50203
Your
<interceptor-ref name="validation"/>
is self-closed, then the
<param name="excludeMethods">
input,back,cancel,browse
</param>
will never be read.
Validation Interceptor
should run after Params Interceptor
, as shown in the examples from the documentation.
Again, according to the documentation,
This interceptor is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have already been set on the action.
Then, try to do like this:
<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>
If it does not work, post your JSP and Action too.
Hope that helps
Upvotes: 1