Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

The example code for this tutorial, interceptors, is available on for checkout at https://svngithub.com/apache.org/repos/asf/struts/sandbox/trunk/struts2examples/struts-examples

Introduction

So far our tutorials have not delved into the inner workings of the Struts 2 framework. But in this tutorial we'll introduce a key set of classes the Struts 2 framework relies upon to do most of the work whenever an Action is executed. In this tutorial's example project there is a register link that is mapped in the Struts XML configuration file (struts.xml) to the execute method of class Register. Before that execute method is called much work is done behind the scenes by the Struts 2 framework. For example:

...

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
xml
xml
titleSpecify Specific Interceptors For An Actionxml


<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>

...

Note the param nodes. These nodes are used to provide a value to the setLogEnabled and setLogLevel methods of the exception interceptor Exception Interceptor. Providing the values of true and ERROR will cause the Struts 2 framework to log any exceptions not caught by the application's code and to log those exceptions at the ERROR level.

...

Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info

INFO: Executed action //register!execute took 177 ms.

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
xml
xml
titleSpecify Specific Interceptors For A Packagexml


<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> 

...