Disable execution of an Interceptor

1.6k views Asked by At

I am working on Struts 2. During the implementation of an Interceptor, I came across to a question:

is it possible to stop the execution of an Interceptor all together via configuration or any other way ?

1

There are 1 answers

0
Andrea Ligios On

There are many ways to decide when a Struts2 Action must run through an Interceptor or not... the easiest are most likely:

  1. Programmatically - Interceptor Configuration

    Define the same action multiple times, each one running with a different interceptor stacks (both defined globally at package level or, like in the following example, in the action configuration itself), one containing your interceptor, the other without it:

    <action name="actionWithInterceptor" class="foo.bar.actions.MyAction">
        <interceptor-ref name="myCustomInterceptor" />
        <interceptor-ref name="defaultStack" />
        <result>/view.jsp</result>
    </action>
    
    <action name="actionWithoutInterceptor" class="foo.bar.actions.MyAction">
     <!--   <interceptor-ref name="myCustomInterceptor" /> -->
        <interceptor-ref name="defaultStack" />
        <result>/view.jsp</result>
    </action>
    
  2. Programmatically - Method Filtering

    MethodFilterInterceptor is an abstract Interceptor used as a base class for interceptors that will filter execution based on method names according to specified included/excluded method lists.

    Settable parameters are as follows:

    • excludeMethods - method names to be excluded from interceptor processing
    • includeMethods - method names to be included in interceptor processing

    Extend this abstract interceptor in your custom interceptor, and define the methods that will filter the interceptor execution in the configuration . In the example, every action method named foo(), or bar(), or starting with withoutCustom like withoutCustomMethod() will avoid running the interceptor, all the other methods instead will run it :

    <interceptor-stack name="myCustomStack">
    
        <interceptor-ref name="myCustomInterceptor">
            <param name="excludeMethods">foo,bar,withoutCustom*</param>
        </interceptor-ref>
    
        <interceptor-ref name="exception"/>
        <interceptor-ref name="alias"/>
        <interceptor-ref name="params"/>
        <interceptor-ref name="servletConfig"/>
        <interceptor-ref name="prepare"/>
        <interceptor-ref name="i18n"/>
        <interceptor-ref name="chain"/>
        <interceptor-ref name="modelDriven"/>
        <interceptor-ref name="fileUpload"/>
        <interceptor-ref name="staticParams"/>
        <interceptor-ref name="params"/>
        <interceptor-ref name="conversionError"/>
        <interceptor-ref name="validation">
            <param name="excludeMethods">myValidationExcudeMethod</param>
        </interceptor-ref>
        <interceptor-ref name="workflow">
            <param name="excludeMethods">myWorkflowExcludeMethod</param>
        </interceptor-ref>
    </interceptor-stack>
    
  3. Dynamically - From within the Interceptor

    Do it in the Interceptor logic, basing on

    • a request parameter:

      if (request.get(MY_REQUEST_PARAM)!=null && 
          ((String) request.get(MY_REQUEST_PARAM)[0]).equals("myVal")) { ...
      
    • a session parameter:

      if (session.get(MY_SESSION_PARAM)!=null && 
          session.get(MY_SESSION_PARAM).equals("myVal")) { ...
      
    • an Interface extended by your Action:

      if(action instanceof MyStuffAware) { ...
      

Etcetera...