Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Sometime the Struts 2 default stack of interceptors are not exactly what you need for a particular action. You may want to use interceptors that are not part of the Struts 2 default stack. For an individual Action or for the entire package of Actions, you can specify a different stack of interceptors that the Action or package should use. Below is how you would specify that the register Action should use both the logger and timer interceptors in addition to the interceptors provided by the default stack.

Code Block
XMLxmlXML
xml
titleSpecify Specific Interceptors For An Action

<action name="register" class="org.apache.struts.register.action.Register" method="execute">
	<interceptor-ref name="timer" />
	<interceptor-ref name="logger" />
	<interceptor-ref name="defaultStack">
		<param name="exception.logEnabled">true</param>
		<param name="exception.logLevel">ERROR</param>
	</interceptor-ref>
	<result name="success">thankyou.jsp</result>
	<result name="input">register.jsp</result>
</action>



The logger interceptor logs the start and end of the execution of an Action. The timer interceptor logs the amount of time (in milliseconds) for execution of the Action. These two interceptors used together can provide developers useful feedback.

...

If you wanted to have the logger and timer interceptors executed for all Actions in a package you would use the following in struts.xml:

Code Block
XMLxmlXML
xml
titleSpecify Specific Interceptors For A Package

<package name="basicstruts2" extends="struts-default" > 


       <interceptors> 

         <interceptor-stack name="appDefault"> 

            <interceptor-ref name="timer" /> 

            <interceptor-ref name="logger" /> 

            <interceptor-ref name="defaultStack" /> 

         </interceptor-stack> 

        </interceptors> 

         

        <default-interceptor-ref name="appDefault" /> 

       <!-- rest of package omitted --> 


</package> 

In the code above we use the interceptors node to define a new stack of interceptors that includes the timer, logger, and defaultStack interceptors. We give this new interceptor stack a name of appDefault. Then we use the default-interceptor-ref node to specify that for all Actions defined inside this package node the appDefault stack of interceptors are to be used. Thus the timer and logger interceptor will be executed for each Action in this package.

...