Struts2拦截器的使用

xiaoxiao2026-03-15  11

[/code]如何使用struts2拦截器,或者自定义拦截器。特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了struts2自带的checkbox拦截器):[code="xml"]<interceptor-ref name="checkbox"> <param name="uncheckedValue">0</param></interceptor-ref><interceptor-ref name="defaultStack"/>(必须加,否则出错) 在struts.xml里面定义全局的配置设置 <package name="struts-shop" extends="struts-default"> <interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="checkbox"> <param name="uncheckedValue">0</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/>(这句是设置所有Action自动调用的拦截器堆栈) </package> struts-action.xml里面配置Action如下: <package name="LogonAdmin" extends="struts-shop">(这里扩展struts.xml里面定义的配置就可以了) <action name="logon" class="logonAction"> <result>/jsp/smeishop/admin/index.jsp</result> <result name="error">/jsp/smeishop/admin/logon.jsp</result> <result name="input">/jsp/smeishop/admin/logon.jsp</result> </action> <action name="logout" class="logoutAction"> <result>/jsp/smeishop/admin/logon.jsp</result> </action> </package> 你的拦截器可以正常工作了!!HOHO 如何自定义一个拦截器? 自定义一个拦截器需要三步: 1 自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。 2 在strutx.xml中注册上一步中定义的拦截器。 3 在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。 Interceptor接口声明了三个方法: public interface Interceptor extends Serializable { void destroy(); void init(); String intercept(ActionInvocation invocation) throws Exception;} Init方法在拦截器类被创建之后,在对Action镜像拦截之前调用,相当于一个post-constructor方法,使用这个方法可以给拦截器类做必要的初始话操作。 Destroy方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源。 Intercept是拦截器的主要拦截方法,如果需要调用后续的Action或者拦截器,只需要在该方法中调用invocation.invoke()方法即可,在该方法调用的前后可以插入Action调用前后拦截器需要做的方法。如果不需要调用后续的方法,则返回一个String类型的对象即可,例如Action.SUCCESS。 另外AbstractInterceptor提供了一个简单的Interceptor的实现,这个实现为: public abstract class AbstractInterceptor implements Interceptor { public void init() { } public void destroy() { } public abstract String intercept(ActionInvocation invocation) throws Exception;} 我们尝试编写一个Session过滤用的拦截器,该拦截器查看用户Session中是否存在特定的属性(LOGIN属性)如果不存在,中止后续操作定位到LOGIN,否则执行原定操作,代码为: 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> <interceptor name="login" class="com.jpleasure.teamware.util.CheckLoginInterceptor"/> <interceptor-stack name="teamwareStack"> <interceptor-ref name="login"/> <interceptor-ref name="defaultStack"/> </interceptor-stack></interceptors> 将上述拦截器设定为默认拦截器: <default-interceptor-ref name="teamwareStack"/> 这样在后续同一个package内部的所有Action执行之前都会被login拦截。 每一个拦截器都可以配置参数,有两种方式配置参数,一是针对每一个拦截器定义参数,二是针对一个拦截器堆栈统一定义所有的参数,例如: <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> 每一个拦截器都有两个默认的参数: excludeMethods - 过滤掉不使用拦截器的方法和 includeMethods – 使用拦截器的方法。 需要说明的几点: 1 拦截器执行的顺序按照定义的顺序执行,例如: <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 thisWillRunNextInterceptor thisWillRunFirstInterceptor 2 使用默认拦截器配置每个Action都需要的拦截器堆栈,例如: <action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/> <result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action> 可以按照如下的方式定义: <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" class="tutorial.Login"> <result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action> 3 如何访问HttpServletRequest,HttpServletResponse或者HttpSession 有两种方法可以达到效果,使用ActionContext: Map attibutes = ActionContext.getContext().getSession(); 或者实现相应的接口: HttpSession SessionAware HttpServletRequest ServletRequestAware HttpServletResponse ServletResponseAware --------------------------------------------------------------------- 2. 方法过滤拦截器 默认情况下我们为某个Action定义了拦截器,则这个拦截器会拦截该Action的所有方法。有些情况下,我们无需拦截所有的方法,此时就需要使用方法过滤拦截器。 方法过滤拦截器使用方法与普通的拦截器没什么区别: 方法过滤拦截器类继承MethodFilterInterceptor 重写的是doIntercept(ActionInvocation invacation)方法 <action/>配置中: <!--放入上面自定义的拦截器--> <interceptor-ref name="isLogin"> <!-- 指定login和register方法不需要被拦截--> <param name="excludeMethods">login,register</param> <!-- 指定execute方法需要被拦截--> <param name="includeMethods">execute</param> </interceptor> 2. 方法过滤拦截器 默认情况下我们为某个Action定义了拦截器,则这个拦截器会拦截该Action的所有方法。有些情况下,我们无需拦截所有的方法,此时就需要使用方法过滤拦截器。 方法过滤拦截器使用方法与普通的拦截器没什么区别: 方法过滤拦截器类继承MethodFilterInterceptor 重写的是doIntercept(ActionInvocation invacation)方法 <action/>配置中: <!--放入上面自定义的拦截器--> <interceptor-ref name="isLogin"> <!-- 指定login和register方法不需要被拦截--> <param name="excludeMethods">login,register</param> <!-- 指定execute方法需要被拦截--> <param name="includeMethods">execute</param> </interceptor> [color=red] [b]注明: 在上面有一个type=redirect-action 的result类型,主要是用于Action之间跳转和传参。 经过我测试 (发现我的) 现在实现这个功能的type是redirectAction[/b][/color]
转载请注明原文地址: https://www.6miu.com/read-5045951.html

最新回复(0)