教你如何使用Struts2拦截器并且定义自己的拦截器
看到网上很多介绍关于拦截器的文章,感觉都不错,但是都没有很详细全面的介绍,所以我就博众家之长,呵呵,写了篇关于struts拦截器的东西。
拦截器的工作原理如上图,在工作的时候每一个Action请求都被包装在一堆拦截器的内部。拦截器可以在Action执行直线做相似的操作也可以在Action执行直后做回收操作。
每一个Action既可以将操作转交给下面的拦截器,Action也可以直接退出操作返回客户既定的画面。
下面我们来讲讲如何使用struts2拦截器,或者是自定义拦截器。
先说说使用Struts自带的拦截器:
在Struts2中已经在struts-default.xml中预定义了一些自带的拦截器,如timer、params等。如果在<package>标签中继承struts-default,则当前package就会自动拥有struts-default.xml中的所有配置。代码如下:
<package name="demo" extends="struts-default" > ... </package>
在struts-default.xml中有一个默认的引用,在默认情况下(也就是<action>中未引用拦截器时)会自动引用一些拦截器。这个默认的拦截器引用如下:
<interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="i18n"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref></interceptor-stack>
<action name="first" /></action>
<action name="first" /> <interceptor-ref name="logger"/></action>
package action;import com.opensymphony.xwork2.ActionSupport;public class FirstAction extends ActionSupport { public String execute() throws Exception { Thread.sleep(2000); // 延迟2秒 return null; }}<s:form action="first" namespace="/test"> <s:textfield name="name"/> <s:submit/></s:form>
<action name="first" /> <param name="who">比尔</param> <interceptor-ref name="params"/> <interceptor-ref name="static-params"/></action>
<package name="demo" extends="struts-default" > <interceptors> <interceptor-stack name="mystack"> <interceptor-ref name="timer" /> <interceptor-ref name="logger" /> <interceptor-ref name="params" /> <interceptor-ref name="static-params" /> </interceptor-stack> </interceptors> <action name="first" name="code">public interface Interceptor extends Serializable { void destroy(); void init(); String intercept(ActionInvocation invocation) throws Exception;}public abstract class AbstractInterceptor implements Interceptor { public void init() { } public void destroy() { } public abstract String intercept(ActionInvocation invocation) throws Exception;}public class CheckLoginInterceptor extends AbstractInterceptor { public static final String LOGIN_KEY = "LOGIN"; public static final String LOGIN_PAGE = "global.login"; public String intercept(ActionInvocation actionInvocation) throws Exception { System.out.println("begin check login interceptor!"); // 对LoginAction不做该项拦截 Object action = actionInvocation.getAction(); if (action instanceof LoginAction) { System.out.println("exit check login, because this is login action."); return actionInvocation.invoke(); } // 确认Session中是否存在LOGIN Map session = actionInvocation.getInvocationContext().getSession(); String login = (String) session.get(LOGIN_KEY); if (login != null && login.length() > 0) { // 存在的情况下进行后续操作。 System.out.println("already login!"); return actionInvocation.invoke(); } else { // 否则终止后续操作,返回LOGIN System.out.println("no login, forward login page!"); return LOGIN_PAGE; } }}<interceptors> <interceptorname="login" name="code"><default-interceptor-ref name="teamwareStack"/>
<package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" name="code"><package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" name="code"><interceptor-ref name="validation"> <param name="excludeMethods">myValidationExcudeMethod</param></interceptor-ref><interceptor-ref name="workflow"> <param name="excludeMethods">myWorkflowExcludeMethod</param></interceptor-ref>
<interceptor-ref name="defaultStack"> <param name="validation.excludeMethods">myValidationExcludeMethod</param> <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param></interceptor-ref>
<interceptor-stack name="xaStack"> <interceptor-ref name="thisWillRunFirstInterceptor"/> <interceptor-ref name="thisWillRunNextInterceptor"/> <interceptor-ref name="followedByThisInterceptor"/> <interceptor-ref name="thisWillRunLastInterceptor"/></interceptor-stack>
thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptorthisWillRunFirstInterceptor
<action name="login" name="code"><interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/> </interceptor-stack></interceptors> <default-interceptor-ref name="myStack"/> <action name="login" name="code">Map attibutes = ActionContext.getContext().getSession();
HttpSession SessionAwareHttpServletRequest ServletRequestAwareHttpServletResponse ServletResponseAware