Reputation: 267077
I'm trying to build a javascript / ajax uploader which submits a form / file upload to an action. However when the form is submitted, the validation interceptor prevents my action from being run for some reason, and returns 'input' as the result. I'm not sure why, but I'd like it to stop.
How can I disable the validation interceptor only for MyAction.execute()
? Here's my interceptors code from struts.xml
:
<interceptors>
<interceptor name="appInit"
class="com.example.myApp.interceptors.AppInit">
</interceptor>
<interceptor-stack name="appDefault">
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="appInit" />
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
<param name="params.excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*,submit</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
Upvotes: 3
Views: 3121
Reputation: 75916
The easiest way (as a quick glance at the docs reveal) is to place the AnnotationValidationInterceptor
in you stack, and annotate your method with @org.apache.struts2.interceptor.validation.SkipValidation
Upvotes: 4
Reputation: 51711
Here's how you could go about bypassing validation for a specific Action. (I'm assuming you wouldn't want to disable the Interceptor for the entire application.)
<action name="MyAction" class="pkg.path.to.MyAction">
<interceptor-ref name="appDefault">
<param name="validation.excludeMethods">execute</param>
<param name="workflow.excludeMethods">execute</param>
</interceptor-ref>
<result>Success.jsp</result>
</action>
The Validation interceptor only logs the errors. It's the Workflow interceptor that checks if any validation errors were logged and if yes redirects to the view mapped to the input
result code.
EDIT:
I think the nested stacks are causing trouble with the syntax for parameter override and I've never tried Update: The above works. :)<stack-name>.<interceptor-name>.excludeMethods
before. You may give it a try though.
But, instead of trying to exclude execute()
you could rename it to input()
(which is already on the exclude list) and add method="input"
to your <action>
. It kinda makes sense for a file upload too.
You could also override validate()
for your Action (though I would personally prefer to do it declaratively through struts.xml
)
public class MyAction extends ActionSupport {
...
@Override
public void validate() {
setFieldErrors(null);
}
}
Upvotes: 4