struts中拦截器拦截Action中的execute方法后的具体拦截流程
2016-09-13 16:51
656 查看
我开始对拦截器的整个拦截流程很难理解,网上都是解释拦截器里面套拦截器,但是没有将讲拦截器的具体流程,下面通过一个图来讲解一下具体流程,如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201609/5a7d7370b068a4fb5faa762fe36012ed)
可以看到其实拦截器就是和servlet中的filter的类似的,都是在请求方法Action之前先验证一下,这就是拦截器的一种典型的应用,验证用户是否登陆,我们通常会对所有的请求都添加拦截器(方法:修改默认的拦截器,自定义自己的myDefaultInterceptorStack),将所有的请求都通过拦截器,但是请注意:本身是登陆的页面不需要经过拦截器
具体看如下代码:
package com.shengsiyuan.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.shengsiyuan.struts2.LoginAction;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
if(LoginAction.class==invocation.getAction().getClass()){//要将本身就是登录界面的action排除在interceptor之外,不然无论如何也是转向错误界面Aciton.LOGIN到error.jsp
return invocation.invoke();
}
// TODO Auto-generated method stub
Map map=
invocation.getInvocationContext().getSession();
if(null==map.get("userinfo")){
return Action.LOGIN;//除了登录的action,其他action都要加上拦截器
}
return null;
}
}
可以看到其实拦截器就是和servlet中的filter的类似的,都是在请求方法Action之前先验证一下,这就是拦截器的一种典型的应用,验证用户是否登陆,我们通常会对所有的请求都添加拦截器(方法:修改默认的拦截器,自定义自己的myDefaultInterceptorStack),将所有的请求都通过拦截器,但是请注意:本身是登陆的页面不需要经过拦截器
具体看如下代码:
package com.shengsiyuan.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.shengsiyuan.struts2.LoginAction;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
if(LoginAction.class==invocation.getAction().getClass()){//要将本身就是登录界面的action排除在interceptor之外,不然无论如何也是转向错误界面Aciton.LOGIN到error.jsp
return invocation.invoke();
}
// TODO Auto-generated method stub
Map map=
invocation.getInvocationContext().getSession();
if(null==map.get("userinfo")){
return Action.LOGIN;//除了登录的action,其他action都要加上拦截器
}
return null;
}
}
相关文章推荐
- Struts 2 methodfilterinterceptor--拦截方法的拦截器
- 第五课 Struts的控制器【续】Action类的execute()方法
- struts学习笔记--解决了struts action中的execute方法不执行的问题
- 第五课 Struts的控制器【续】Action类的execute()方法
- 使用Spring内置拦截器为Struts的Action在每个方法调用之前打印一句话
- Struts 2 methodfilterinterceptor--拦截方法的拦截器
- struts2 拦截器拦截action中指定方法
- Struts2.0学习笔记---拦截器拦截方法
- 如何在struts中的action的execute方法()中弹出对话框
- struts2框架中Action必须实现execute方法?和Struts参数传递
- Struts 2 methodfilterinterceptor--拦截方法的拦截器
- struts2.0在action里获得servlet对象方法
- 为struts的action添加到SPRING的AOP日志拦截器
- 在Struts action中利用.do跳转到另一action传值方法
- 为struts的action添加到SPRING的AOP日志拦截器
- 在使用struts的DispatchAction的时候出现Action找不到指定方法的错误
- struts表单中多个submit的处理方法:org.apache.struts.actions.DispatchAction
- struts中actionFrom传递日期出错的解决方法
- 关于struts如何在action中实现多个处理方法
- struts ,action,跳转变化页面方法