我们可以看一下ServletConfigInterceptor的源码
public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics {
private static final long serialVersionUID = 605261777858676638L;
/**
* Sets action properties based on the interfaces an action implements. Things like application properties,
* parameters, session attributes, etc are set based on the implementing interface.
*
* @param invocation an encapsulation of the action execution state.
* @throws Exception if an error occurs when setting action properties.
*/
public String intercept(ActionInvocation invocation) throws Exception {
final Object action = invocation.getAction();
final ActionContext context = invocation.getInvocationContext();
if (action instanceof ServletRequestAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
((ServletRequestAware) action).setServletRequest(request);
}
if (action instanceof ServletResponseAware) {
HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
((ServletResponseAware) action).setServletResponse(response);
}
if (action instanceof ParameterAware) {
((ParameterAware) action).setParameters((Map)context.getParameters());
}
if (action instanceof ApplicationAware) {
((ApplicationAware) action).setApplication(context.getApplication());
}
if (action instanceof SessionAware) {
((SessionAware) action).setSession(context.getSession());
}
if (action instanceof RequestAware) {
((RequestAware) action).setRequest((Map) context.get("request"));
}
if (action instanceof PrincipalAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
if(request != null) {
// We are in servtlet environment, so principal information resides in HttpServletRequest
((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
}
}
if (action instanceof ServletContextAware) {
ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
((ServletContextAware) action).setServletContext(servletContext);
}
return invocation.invoke();
}
}
先不看intercept中的具体实现,我们发现ServletConfigInterceptor继承了一个AbstractInterceptor的一个抽象拦截器,我们向其追溯
public abstract class AbstractInterceptor implements Interceptor {
/**
* Does nothing
*/
public void init() {
}
/**
* Does nothing
*/
public void destroy() {
}
/**
* Override to handle interception
*/
public abstract String intercept(ActionInvocation invocation) throws Exception;
}
实现了一个叫做Interceptor的接口,我们再次向上追溯
public interface Interceptor extends Serializable {
/**
* Called to let an interceptor clean up any resources it has allocated.
*/
void destroy();
/**
* Called after an interceptor is created, but before any requests are processed using
* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
* the Interceptor a chance to initialize any needed resources.
*/
void init();
/**
* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
*
* @param invocation the action invocation
* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
*/
String intercept(ActionInvocation invocation) throws Exception;
}
只有三个方法,destroy()、init(),和servlet一样,有着初始化和销毁的方法,可见其设计模式是单例的。
从这张图中,我们不难看出,在生成代理对象之后,执行了三次拦截器,再到相应的Action方法,返回result,生成模板(JSP、FreeMaker等),再去倒序执行拦截器,而其中的内容、数据,则是由ActionInvocation承载的,其也是inercept方法中传入的参数。
String intercept(ActionInvocation invocation) throws Exception;
示例一个自定义的拦截器:
A.创建一个普通类 继承自AbstractInterceptor,实现抽象方法intercept
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* 自定义拦截器:
* A.创建一个普通类 继承自AbstractInterceptor,实现抽象方法intercept
*
*
* @author Keo.Zhao
*
*/
public class Demo1Interceptor extends AbstractInterceptor{
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Demo1Interceptor拦截了---执行动作方法之前");
//放行:如果有下一个拦截器,就前往下一个拦截器,如果没有了,就到达动作方法
String rtString = invocation.invoke();
// System.out.println(rtString);
// System.out.println("Demo1Interceptor拦截了---执行动作方法之后");
return rtString;
}
}
通过invocation.invoke();放行,而Struts2中的各种拦截器也是根据不同的功能与需求,以不同的方式重写intercept
B.struts.xml配置拦截器
<package name="p1" extends="struts-default">
<!-- 声明自定义拦截器 -->
<interceptors>
<interceptor name="Demo1Interceptor1" class="cn.test.interceptor.Demo1Interceptor1"></interceptor>
<interceptor name="Demo1Interceptor2" class="cn.test.interceptor.Demo1Interceptor2"></interceptor>
</interceptors>
<action name="action1" class="cn.test.action.Demo1Action"method="save">
<!-- 使用自定义拦截器:当配置了一个任何拦截器,默认的拦截器栈就不会工作了 当有多个拦截器时,
时由引用配置决定执行的顺序。注意:执行顺序与声明无关 -->
<interceptor-ref name="Demo1Interceptor1"></interceptor-ref>
<interceptor-ref name="Demo1Interceptor2"></interceptor-ref>
<result name="success">/Demo1.jsp</result>
<result name="input">/Demo2.jsp</result>
</action>
</package>
如上述,当配置了自定义的拦截器,默认的拦截器栈就不会工作了,那我们如何保证自定义的拦截器与默认的拦截器都可以有效工作呢?
从AbstractInterceptor入手不难发现其子类中有一个抽象类 -— — MethodFilterInterceptor,看看它的描述
* <!-- START SNIPPET: javadoc -->
*
* MethodFilterInterceptor is an abstract <code>Interceptor</code> used as
* a base class for interceptors that will filter execution based on method
* names according to specified included/excluded method lists.
methodfilterinterceptor是一个抽象的拦截器,将滤波器的基础上执行基类的方法根据指定的包含/排除方法列表的名称。
言而言之,就是它可以排除不需要过滤的方法!
所以,可以让我们自定义的拦截器去实现其中的抽象方法!
public class CheckLoginInterceptor extends MethodFilterInterceptor {
public String doIntercept(ActionInvocation invocation) throws Exception {
String rtValue = invocation.invoke();
return rtValue;
}
}
<package name="p2" extends="struts-default">
<interceptors>
<interceptor name="checkLoginInterceptor" class="com.itheima.web.interceptor.CheckLoginInterceptor1" />
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="checkLoginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
<global-results>
<result name="input">/login.jsp</result>
</global-results>
<action name="login" class="com.itheima.web.action.Demo2Action" method="login">
<interceptor-ref name="myDefaultStack">
<!-- 在引用自定义拦截器栈的时候,给指定的拦截器注入参数。方式就是:拦截器名称.属性名称 -->
<param name="checkLoginInterceptor1.excludeMethods">login</param>
</interceptor-ref>
<result type="redirectAction">showMain</result>
</action>
<action name="showMain" class="com.itheima.web.action.Demo2Action" >
<result>/main.jsp</result>
</action>
<action name="showOther" class="com.itheima.web.action.Demo2Action" >
<result>/otherpage.jsp</result>
</action>
</package>