Understanding Interceptors
- Interceptors can execute code before and after an Action is invoked.
- Most of the framework's core functionality is implemented as Interceptors.
- Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more, are all implemented with the help of Interceptors.
- Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.
- Interceptors can be configured on a per-action basis.
- Your own custom Interceptors can be mixed-and-matched with the Interceptors bundled with the framework.
- Interceptors can also change the state of an Action before it executes.
---------------------------------------------------------------------------------------------------------------
Configuring Interceptors
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".."/>
<interceptor name="logger" class=".."/>
</interceptors>
<action name="login"
class="tutorial.Login">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
<result name="input">login.jsp</result>
<result name="success"
type="redirectAction">/secure/home</result>
</action></package>
---------------------------------------------------------------------------------------------------------------
Stacking Interceptors
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".."/>
<interceptor name="logger" class=".."/>
<interceptor-stack name="myStack">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
</interceptor-stack>
</interceptors>
<action name="login"
class="tutuorial.Login">
<interceptor-ref name="myStack"/>
<result name="input">login.jsp</result>
<result name="success"
type="redirectAction">/secure/home</result>
</action>
</package>
---------------------------------------------------------------------------------------------------------------
Writing Interceptors
Interceptor interface
- Interceptors must implement the com.opensymphony.xwork2.interceptor.Interceptor interface.
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
- The init method is called the after interceptor is instantiated and before calling intercept.
- This is the place to allocate any resources used by the interceptor.
- The intercept method is where the interceptor code is written.
- Just like an action method, intercept returns a result used by Struts to forward the request to another web resource.
AbstractInterceptor
- The AbstractInterceptor class provides an empty implementation of init and destroy, and can be used if these methods are not going to be implemented.
Mapping
- Interceptors are declared using the interceptor element, nested inside the interceptors element. Example from struts-default.xml:
struts.xml
----------
<struts>
...
<package name="struts-default">
<interceptors>
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
...
</interceptors>
</package>
...
</struts>
SimpleInterceptor
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class SimpleInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
MyAction action = (MyAction)invocation.getAction();
action.setDate(new Date());
String result =invocation.invoke();
//after execute interceptor code
return result;
}
}
- Interceptors can execute code before and after an Action is invoked.
- Most of the framework's core functionality is implemented as Interceptors.
- Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more, are all implemented with the help of Interceptors.
- Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.
- Interceptors can be configured on a per-action basis.
- Your own custom Interceptors can be mixed-and-matched with the Interceptors bundled with the framework.
- Interceptors can also change the state of an Action before it executes.
---------------------------------------------------------------------------------------------------------------
Configuring Interceptors
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".."/>
<interceptor name="logger" class=".."/>
</interceptors>
<action name="login"
class="tutorial.Login">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
<result name="input">login.jsp</result>
<result name="success"
type="redirectAction">/secure/home</result>
</action></package>
---------------------------------------------------------------------------------------------------------------
Stacking Interceptors
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".."/>
<interceptor name="logger" class=".."/>
<interceptor-stack name="myStack">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
</interceptor-stack>
</interceptors>
<action name="login"
class="tutuorial.Login">
<interceptor-ref name="myStack"/>
<result name="input">login.jsp</result>
<result name="success"
type="redirectAction">/secure/home</result>
</action>
</package>
---------------------------------------------------------------------------------------------------------------
Writing Interceptors
Interceptor interface
- Interceptors must implement the com.opensymphony.xwork2.interceptor.Interceptor interface.
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
- The init method is called the after interceptor is instantiated and before calling intercept.
- This is the place to allocate any resources used by the interceptor.
- The intercept method is where the interceptor code is written.
- Just like an action method, intercept returns a result used by Struts to forward the request to another web resource.
AbstractInterceptor
- The AbstractInterceptor class provides an empty implementation of init and destroy, and can be used if these methods are not going to be implemented.
Mapping
- Interceptors are declared using the interceptor element, nested inside the interceptors element. Example from struts-default.xml:
struts.xml
----------
<struts>
...
<package name="struts-default">
<interceptors>
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
...
</interceptors>
</package>
...
</struts>
SimpleInterceptor
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class SimpleInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
MyAction action = (MyAction)invocation.getAction();
action.setDate(new Date());
String result =invocation.invoke();
//after execute interceptor code
return result;
}
}
No comments:
Post a Comment