Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Struts Validation Interceptor?

Tags:

java

struts2

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>
like image 871
Ali Avatar asked Nov 30 '25 00:11

Ali


1 Answers

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);
  }
}
like image 106
Ravi K Thapliyal Avatar answered Dec 02 '25 14:12

Ravi K Thapliyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!