3

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>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Ali
  • 261,656
  • 265
  • 575
  • 769
  • 3
    Care (whoever) to explain downvotes (to question and answers) and vote to close? – leonbloy May 14 '13 at 20:46
  • 1
    @leonbloy There's a particular user with an attitude problem; the ultimate solution is not to worry about it, 'cuz haters gonna hate. – Dave Newton May 14 '13 at 21:08
  • 1
    Ehh he he.. Somebody down voted all of us!! And the absence of any contradicting point of view proves how much he understood the question or understands Struts 2 in general. I guess he too has a validation problem. :) – Ravi K Thapliyal May 14 '13 at 21:11
  • Possible duplicate of http://stackoverflow.com/questions/14397240/how-to-configure-skipvalidation-by-xml-in-struts2 – Roman C May 15 '13 at 08:27

2 Answers2

4

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • Any ideas how to do that without annotations? – Ali May 14 '13 at 19:50
  • +1. And considering the fact that in most cases one would extend [*struts-default.xml*](http://struts.apache.org/release/2.3.x/docs/struts-defaultxml.html) package, which **already** contains configuaration for the [*AnnotationValidationInterceptor*](http://struts.apache.org/development/2.x/struts2-core/apidocs/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.html), adding `SkipValidation` annotation is all you need. Quick and simple. Thanks for the tip ) – informatik01 Jul 18 '14 at 14:20
4

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 <stack-name>.<interceptor-name>.excludeMethods before. You may give it a try though. Update: The above works. :)

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);
  }
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89