拦截器与过滤器
拦截器是对调用的Action起作用,它提供了一种机制可以使开发者定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式,很多业务逻辑都是靠拦截实现的,比如校验,验证登录权限(比如下载时跳转到登陆页面)等等。 过滤器是对整个的请求过程起作用!换句话说就是拦截器没有过滤器的范围广。过滤器是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符(聊天室经常用到的,一些骂人的话,比如判断用户提交的数据是否存在非法字符等等。
Struts2拦截器是Struts2中的一个很重要的功能,本质是代理模式。本文将从概念开始,为大家讲解Struts2拦截器的实现原理以及如何定义等等内容。
一、理解Struts2拦截器
1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现。
2. 拦截器栈(Interceptor Stack)。Struts2拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用。
二、执行责任
这个执行职责有3种选择:
1) 中止整个执行,直接返回一个字符串作为resultCode
2) 通过递归调用负责调用堆栈中下一个Interceptor的执行
3) 如果在堆栈内已经不存在任何的Interceptor,调用Action
三、实现Struts2拦截器原理
Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts2会查找配置文件,并根据其配置实例化相对应的拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器。
四、定义Struts2拦截器
Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口。该接口声明了3个方法
voidinit();voiddestroy();String intercept(ActionInvocation invocation)throws Exception;
不过,struts中又提供了几个抽象类来简化这一步骤。其中,init和destroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。
intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑。public abstract classAbstractInterceptorimplementsInterceptor;public abstract classMethodFilterInterceptorextendsAbstractInterceptor;
其中AbstractInterceptor提供了init()和destroy()的空实现,使用时只需要覆盖intercept()方法;都是模板方法实现的;而MethodFilterInterceptor则提供了includeMethods和excludeMethods两个属性,用来过滤执行该过滤器的action的方法。可以通过param来加入或者排除需要过滤的方法。
一般来说,拦截器的写法都差不多。看下面的示例:
package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public classMyInterceptorimplementsInterceptor{publicvoiddestroy(){// TODO Auto-generated method stub}publicvoidinit(){// TODO Auto-generated method stub}public String intercept(ActionInvocation invocation)throws Exception { System.out.println("Action执行前插入 代码"); //执行目标方法 (调用下一个拦截器, 或执行Action) final String res = invocation.invoke(); System.out.println("Action执行后插入 代码"); return res; }
Struts2拦截器需要在struts.xml中声明,如下struts.xml配置文件,配置Struts2拦截器
/index.jsp /success.jsp
拦截器全套简单例子:
struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /*
/home.jsp /file.jsp /login.jsp
package com.entity;/** * 用户类 * @author asus * */public class Users { /** 属性 */ private String name; private String password; /** 构造方法 */ public Users() { super(); } public Users(String name, String password) { super(); this.name = name; this.password = password; } /** javaBean */ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/** * 拦截器类 * 作用:计算用户从开始登录到结束登录消耗的毫秒数 * @author asus * */public class TimeConsumingInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("执行顺序:1进入TimeConsumingInterceptor"); //开始时间 long startTime = System.currentTimeMillis(); System.out.println("执行顺序:2输出开始时间startTime:"+startTime); //调用下一个拦截器,如果拦截器不存在,则执行Action。 String result = invocation.invoke(); System.out.println("执行顺序:5输出Action返回的结果:"+result); //结束时间 long endTimen = System.currentTimeMillis(); System.out.println("执行顺序:6输出结束时间endTimen:"+endTimen); //登陆使用时间 System.out.println("登陆使用时间:"+(endTimen-startTime)+"毫秒。。。"); return result; }}
package com.interceptor;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.entity.Users;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;/** * 拦截器类 * 作用:检查用户是否登陆,没有登录则不能向Action发送请求。 * * 测试请url访问:loginUsers.action * @author asus * */public class CheckLoginInterceptor implements Interceptor { @Override/** 销毁的方法 */ public void destroy() { // TODO Auto-generated method stub } @Override/** 初始化方法 */ public void init() { // TODO Auto-generated method stub } @Override/** 拦截器 */ public String intercept(ActionInvocation invocation) throws Exception { System.out.println("执行顺序:3进入CheckLoginInterceptor"); //得到request对象 HttpServletRequest request = ServletActionContext.getRequest(); //取得登陆页面用户输入的账号密码若不为空的话让其通过 String name = request.getParameter("user.name"); String password = request.getParameter("user.password"); //取session中保存的用户登录信息 Users users = (Users) invocation.getInvocationContext().getSession().get("users"); if(name!=null && password!=null){ //若是登陆页面请求Action,则通过 return invocation.invoke(); }else if(users==null){ return Action.INPUT;//input常量 } //若已经登陆,则让其通过访问下一个拦截器,或Action。 return invocation.invoke(); }}
package com.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/** * 拦截器 * 作用:测试定义局部拦截器执行顺序。 * @author asus * */public class OtherInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("局部拦截器"); return invocation.invoke(); }}
package com.struts;import com.entity.Users;import com.opensymphony.xwork2.ActionSupport;/** * 控制器类 * 作用:处理用户的请求 * @author asus * */public class UsersAction extends ActionSupport { /** 属性 */ private Users user; /** 重写execute方法 :此方法作用,为指定处理请求的方法时,默认走此方法*/ public String execute(){ return ""; } /** 登陆验证的方法 */ public String login(){ System.out.println("执行顺序:4进入login()"); if(user!=null){ if(user.getName().equals("admin") && user.getPassword().equals("admin")){ return SUCCESS; } } return LOGIN; } /** JavaBean */ public Users getUser() { return user; } public void setUser(Users user) { this.user = user; } }
登录成功进入首页。。
登陆失败页面。。
全局拦截器:控制台输出登录成功的拦截器执行顺序。
局部拦截器:控制台输出登录成功的拦截器执行顺序。当定义局部拦截器时,则不会再走全局拦截器。