Struts2 自定义拦截器(方法拦截器)
2012-06-15 17:06
363 查看
struts2系统自带了很多拦截器,有时需要我们自己定义,一般有两种方式:
一、实现Interceptor接口
二、继承AbstractInterceptor类,重写intercept()方法即可
此方法更可行,其实AbstractInterceptor类也就是实现了Interceptor接口
三、方法拦截器:继承MethodFilterInterceptor类,重写doIntercept()方法
MethodFilerInterceptor实现方法过滤中用到的两个参数
定义好自定义拦截器后,就要使用自定义拦截器,在struts.xml文档中
一、包内定义拦截器
以下是例子:
struts.xml
redirect.jsp
这种方式的跳转没有bug出现,另外还有一个bug的页面跳转实现,其他时候在写。
一、实现Interceptor接口
public interface Interceptor extends Serializable{ public void init(); public void destroy(); public String intercept(ActionInvocation invocation)(); }实现上述方法
二、继承AbstractInterceptor类,重写intercept()方法即可
此方法更可行,其实AbstractInterceptor类也就是实现了Interceptor接口
invocation.invoke();表示该方法执行完后执行Action的execute()方法或者执行下一个拦截器 |
invocation.getAction(); 可以将该法强制转换为Action的类类型 |
MethodFilerInterceptor实现方法过滤中用到的两个参数
execludeMethods:该参数指定拦截器拒绝拦截的方法列表,多个方法用“,”隔开,指定了这个参数,拦截器不会拦截指定列表中的方法,就是所谓的黑名单 |
includeMethods:该参数指定拦截器需要拦截的方法列表,如果指定了参数,则指定的Action在执行前会被拦截,即白名单。 |
一、包内定义拦截器
<package....> <interceptors> <interceptor name="myinterceptor" class="...."> </interceptor> </interceptors> </package>二、action内使用拦截器
<action .....> <result.....></result> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myinterceptor"></interceptor-ref> </action>主要:可以看出使用了自定义拦截器的action要配置默认拦截器的引用,因为默认拦截器包含了参数的读取、session的管理等功能
以下是例子:
public class PermissionInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { // 取得请求的URL String url = ServletActionContext.getRequest().getRequestURL().toString(); // 验证Session是否过期 if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){ //session过期,转向session过期提示页,最终跳转至登录页面 return "loginPage"; } else { // 对登录与注销请求直接放行,不予拦截 if (url.indexOf("login!login.action")!=-1 || url.indexOf("login!logoff.action")!=-1){ return invocation.invoke(); } else{ UserSession userSession = (UserSession)ActionContext.getContext().getSession().get("userSession"); // 如果user不为null,代表用户已经登录,允许执行action中的方法 if (userSession != null) { return invocation.invoke(); } ActionContext.getContext().put("message", "你没有权限执行该操作"); return "loginPage"; } } } }
struts.xml
<package name="default" extends="struts-default"> <interceptors> <interceptor name="permission" class="com.lining.interceptor.PermissionInterceptor"/> <interceptor-stack name="baseStack"> <interceptor-ref name="permission"/> <interceptor-ref name="exception" /> <interceptor-ref name="alias" /> <interceptor-ref name="servletConfig" /> <interceptor-ref name="i18n" /> <interceptor-ref name="prepare" /> <interceptor-ref name="chain" /> <interceptor-ref name="debugging" /> <interceptor-ref name="scopedModelDriven" /> <interceptor-ref name="modelDriven" /> <interceptor-ref name="fileUpload" /> <interceptor-ref name="checkbox" /> <interceptor-ref name="multiselect" /> <interceptor-ref name="staticParams" /> <interceptor-ref name="actionMappingParams" /> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <!-- 配置方法级别的校验 --> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> <param name="validateAnnotatedMethodOnly">true</param> </interceptor-ref> </interceptor-stack> </interceptors> <!-- 配置默认拦截器栈 --> <default-interceptor-ref name="baseStack" /> <!-- 定义全局result --> <global-results> <!-- 定义名为exception的全局result --> <result name="loginPage">/redirect.jsp</result> </global-results> </package>
redirect.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Login</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript"> alert('登录超时,请重新登录'); parent.parent.window.location = "/index.jsp"; </script> </head> <body> </body> </html>
这种方式的跳转没有bug出现,另外还有一个bug的页面跳转实现,其他时候在写。
相关文章推荐
- struts2中自定义拦截器intercept的相关配置及使用方法
- Struts2 自定义拦截器(方法拦截器)
- struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- struts2自定义拦截器注解配置方法
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- Struts2 自定义拦截器(方法拦截器)
- struts2自定义拦截器和方法拦截器
- 在struts2中配置自定义拦截器放行多个方法
- Struts2拦截器之拦截器的方法过滤
- struts2自定义拦截器
- Struts2 自定义日志 拦截器 interceptor
- Struts2自定义拦截器,检查用户是否登录
- Struts2-16 自定义拦截器
- Struts2自定义拦截器实例—登陆权限验证
- Struts2笔记——自定义拦截器
- Struts2的自定义拦截器