首页 > 代码库 > 菜鸟学Struts2——Interceptors

菜鸟学Struts2——Interceptors

  昨天学习Struts2的Convention plugin,今天利用Convention plugin进行Interceptor学习,虽然是使用Convention plugin进行零配置开发,这只是在Interceptor的使用上,定义Interceptor还是使用strutx.xml配置形式。Interceptors模块如下图: 

技术分享

(1)编写Interceptor

自定义Interceptor可以实现com.opensymphony.xwork2.interceptor.Interceptor接口,也是继承com.opensymphony.xwork2.interceptor.AbstractInterceptor,实际上AbstractInterceptor就是实现了Interceptor接口,并提供init()和destroy()的默认实现(空实现)。这里直接继承AbstractInterceptor,写一个为Action设值的拦截器,代码如下: 

 1 package yaolin.core.interceptor;
 2 import com.opensymphony.xwork2.ActionInvocation;
 3 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 4 import yaolin.core.action.OutAction;
 5 
 6 public class ValueInterceptor extends AbstractInterceptor {
 7     private static final long serialVersionUID = -8801972415271737380L;
 8 
 9     //resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
10     @Override
11     public String intercept(ActionInvocation invocation) throws Exception {
12         OutAction action = null;
13         if (invocation.getAction() instanceof OutAction) {
14             action = (OutAction) invocation.getAction();
15             action.setBefore("before");
16         }
17         invocation.invoke();
18             // invoke all interceptor
19             // resultCode = invokeActionOnly();
20             // executeResult(); 这里已经执行了executeResult(),所以下面的return不会对result产生影响。
21         // continue valueInterceptor
22         if (action != null) {
23             // 上面已经执行了invokeActionOnly(),所以这里的设值不会影响值栈
24             action.setAfter("after");
25         }
26         return invocation.getResultCode();
27     }
28 
29 }

 (2)配置Interceptor

在struts.xml配置这个Interceptor :

 1 <!DOCTYPE struts PUBLIC
 2     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 3     "http://struts.apache.org/dtds/struts-2.3.dtd">
 4 <struts>
 5     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 6     <package name="v">
 7         <interceptors>
 8             <interceptor name="valueInterceptor" class="yaolin.core.interceptor.ValueInterceptor"></interceptor>
 9         </interceptors>
10     </package>
11 </struts>

(3)在Action中使用Interceptor

Action除了execute()方法外有两个属性before和after,并提供getter和setter,在Action中加入@InterceptorRef("valueInterceptor")注解,指定拦截器"valueInterceptor"

 1 package yaolin.core.action;
 2 
 3 import org.apache.struts2.convention.annotation.InterceptorRef;
 4 import org.apache.struts2.convention.annotation.ParentPackage;
 5 
 6 /**
 7  * /out.action
 8  * @author yaolin
 9  */
10 @ParentPackage("v")
11 @InterceptorRef("valueInterceptor")
12 public class OutAction {
13 
14     private String before;
15     private String after;
16     
17     public String execute() {
18         return "success";
19     }
20     
21     public String getBefore() {
22         return before;
23     }
24     public void setBefore(String before) {
25         this.before = before;
26     }
27     public String getAfter() {
28         return after;
29     }
30     public void setAfter(String after) {
31         this.after = after;
32     }
33 }

 注意这里要加入@ParentPackage("v")指定继承的父包,如果这里不指定的话会出现找不到Interceptor的异常。这一段在文档中是这样描述的:

技术分享

包配置(com.opensymphony.xwork2.config.entities.PackageConfig)

页面上直接获取before和after的值,out.jsp如下:

技术分享

 

结果(解释见Interceptor的注释):

技术分享

 

下面是使用xml配置Action时,拦截器的配置例子:

 1 <package name="default" extends="struts-default">
 2    <interceptors>
 3         <interceptor name="timer" class=".."/>
 4         <interceptor name="logger" class=".."/>
 5         <interceptor-stack name="myStack">
 6            <interceptor-ref name="timer"/>
 7            <interceptor-ref name="logger"/>
 8         </interceptor-stack>
 9     </interceptors>
10  
11 <action name="login"
12      class="tutuorial.Login">
13          <interceptor-ref name="myStack"/>
14          <result name="input">login.jsp</result>
15          <result name="success"
16              type="redirectAction">/secure/home</result>
17 </action>
18 </package>

菜鸟学Struts2——Interceptors