菜鸟学Struts2——Interceptors

时间:2023-03-08 22:55:46
菜鸟学Struts2——Interceptors

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

菜鸟学Struts2——Interceptors

(1)编写Interceptor

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

 package yaolin.core.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import yaolin.core.action.OutAction; public class ValueInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -8801972415271737380L; //resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
@Override
public String intercept(ActionInvocation invocation) throws Exception {
OutAction action = null;
if (invocation.getAction() instanceof OutAction) {
action = (OutAction) invocation.getAction();
action.setBefore("before");
}
invocation.invoke();
// invoke all interceptor
// resultCode = invokeActionOnly();
// executeResult(); 这里已经执行了executeResult(),所以下面的return不会对result产生影响。
// continue valueInterceptor
if (action != null) {
// 上面已经执行了invokeActionOnly(),所以这里的设值不会影响值栈
action.setAfter("after");
}
return invocation.getResultCode();
} }

(2)配置Interceptor

在struts.xml配置这个Interceptor :

 <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="v">
<interceptors>
<interceptor name="valueInterceptor" class="yaolin.core.interceptor.ValueInterceptor"></interceptor>
</interceptors>
</package>
</struts>

(3)在Action中使用Interceptor

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

 package yaolin.core.action;

 import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage; /**
* /out.action
* @author yaolin
*/
@ParentPackage("v")
@InterceptorRef("valueInterceptor")
public class OutAction { private String before;
private String after; public String execute() {
return "success";
} public String getBefore() {
return before;
}
public void setBefore(String before) {
this.before = before;
}
public String getAfter() {
return after;
}
public void setAfter(String after) {
this.after = after;
}
}

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

菜鸟学Struts2——Interceptors

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

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

菜鸟学Struts2——Interceptors

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

菜鸟学Struts2——Interceptors

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

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