一、了解Struts2 拦截器【Interceptor】

拦截器的工作原理如图  拦截器是由每一个action请求(request)都包装在一系列的拦截器的内部,通过redirectAction再一次发送请求。

拦截器可以在Action执行直线做相似的操作也可以在Action执行直后做回收操作。

我们可以让每一个Action既可以将操作转交给下面的拦截器,Action也可以直接退出操作返回客户既定的画面。

接下来我们该如何定义一个拦截器:

  自定义一个拦截器如下:

    1、实现Interceptor接口或者继承AbstractInterceptor抽象类。

2、创建一个Struts.xml文件进行定义拦截器。

    3、在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器(<default-interceptor-ref name="myStack"/>),

       这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截<param name="excludeMethods">loginView,login</param>。

①Interceptor接口声明三个方法:

 public class LoginInterceptor implements Interceptor {

     private Map<String,Object> session = null;
public void destroy() { }
public void init() { }
public String intercept(ActionInvocation actionInvocation) throws Exception {
8     Object myAction = actionInvocation.getAction();
if(myAction instanceof UserAction){
System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
//放行
return actionInvocation.invoke();
}else{
System.out.println("你访问的Action是:"+myAction);
} session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
} } 注:该方法可以不加:<param name="excludeMethods">loginView,login</param>

②让它继承 MethodFilterInterceptor:

public class LoginInterceptor extends MethodFilterInterceptor {
private Map<String,Object> session = null;
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
/*
Object myAction = actionInvocation.getAction();
if(myAction instanceof UserAction){
System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
//放行
return actionInvocation.invoke();
}else{
System.out.println("你访问的Action是:"+myAction);
}
*/
session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
}
}
}

③UserAction继承ActionSupport 实现 ModelDriven<User>和SessionAware:

 public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware{

     private Map<String,Object> session = null;
private User user = null;
   //驱动模型
public User getModel() {
this.user = new User();
return this.user;
} public void setSession(Map<String, Object> map) {
this.session = map;
} public String loginView(){
return "loginViewSuccess";
} public String login(){
if ("admin".equals(user.getUserName())&&"123456".equals(user.getUserPassword())){
session.put("user",user);
return this.SUCCESS;
}else{
return this.ERROR;
} }
}

Struts.xml文件中:

<struts>
<package name="myPackage" extends="struts-default"> <interceptors> <interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor">
<!--excludeMethods需要生效的话,自定义的拦截器,不能使用实现Interceptor接口,而是extends MethodFilterInterceptor-->
<param name="excludeMethods">loginView,login</param><!--不用此行时 我们可以配合①使用拦截器-->
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--配置一个默认拦截器,也就是所有的Action都必须使用-->
<default-interceptor-ref name="myStack"/>
<global-results>
<result name="login" type="redirectAction">userAction_loginView</result>
</global-results>
<!--不写method,默认就是execute-->
<action name="indexAction" class="com.nf.action.IndexAction" method="execute">
<result name="success">/WEB-INF/jsp/index.jsp</result>
<!--
<interceptor-ref name="myStack"></interceptor-ref>
-->
<!--注释这里也可以放该代码 只不过每一个action都要放比较麻烦
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action> <action name="otherFunctionAction" class="com.nf.action.OtherFunctionAction">
<!--不写name,默认就是success-->
<result>/WEB-INF/jsp/otherFunction.jsp</result>
</action> <action name="userAction_*" class="com.nf.action.UserAction" method="{1}">
<result name="loginViewSuccess">/WEB-INF/jsp/loginView.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
<result name="success" type="redirectAction">indexAction</result>
<allowed-methods>login,loginView</allowed-methods>
</action>
</package>
</struts>

其中,<param name="excludeMethods">loginView,login</param>  配置的过滤方法,意思是拦截器对其中的方法不起作用。在我这里,loginView是跳转到登录页面的方法。

    login 是验证用户名和密码的方法,在其中会将通过验证的用户名放入session中。

  总结:1.在struts2 中,所有的拦截器都会继承 Interceptor 这个接口。

       2.如果我们没有添加拦截器,struts2 会为我们添加默认拦截器。当然我们要是指定了拦截器,我们自己的拦截器就会取代默认的拦截器,

      那么我们就不能享受默认拦截器提供的一些功能。所以,一般我会把默认拦截器也加上。

      例如,在以上配置项中,action 里面再加上<interceptor-ref name="defaultStack"></interceptor-ref> 

Struts2拦截器 解决登录问题的更多相关文章

  1. 基于SSH2框架Struts2拦截器的登录验证实现(转)

        大象在这里假设你已经弄清楚了Struts2拦截器的基本概念,可以进入实际运用了.那么我们在之前的基础上只需要做下小小的改变,就可以使用Struts2的拦截器机制实现登录的验证.     修改数 ...

  2. Struts2透过自定义拦截器实现登录之后跳转到原页面

    Struts2通过自定义拦截器实现登录之后跳转到原页面 这个功能对用户体验来说是非常重要的.实现起来其实很简单. 拦截器的代码如下: package go.derek.advice; import g ...

  3. Struts2拦截器登录验证

    Struts2拦截器 Struts2拦截器的概念和Spring Mvc拦截器一样. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截 ...

  4. 防止未登录用户操作—struts2拦截器简单实现(转)

    原文地址:http://blog.csdn.net/zhutulang/article/details/38351629 尊重原创,请访问原地址 一般,我们的web应用都是只有在用户登录之后才允许操作 ...

  5. struts2(五)之struts2拦截器与自定义拦截器

    前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...

  6. struts2 拦截器

    拦截器:对Action的访问.可以拦截到Action中某个方法.与过滤器不同,过滤器过滤的是请求.过滤JSP.html.但是拦截器不能拦截jsp.html的访问. Struts2 拦截器在访问某个 A ...

  7. 浅谈Struts2拦截器的原理与实现

    拦截器与过滤器           拦截器是对调用的Action起作用,它提供了一种机制可以使开发者定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行.同时也是提供了 ...

  8. (三)Struts2 拦截器

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:拦截器简介 (百度百科Struts2) Struts2 拦截器是在访问某 ...

  9. Struts2拦截器总结<转>

    由于项目中在登录跳转到其他应用程序模块的时候有用到拦截器,因此查看了一下相关资料. 原文地址:http://blog.csdn.net/sendfeng/article/details/4248120 ...

随机推荐

  1. iOS-跨界面传值和跨应用传值

    跨界面传值 从一个界面将一个结果值传到另一个界面,这个是我们在开发过程中非常常见的一个问题.传值本身并不是一个太复杂的问题,在此主要简述一下常用的传值方法. 我们传值常用的方法主要有四种: 1.属性传 ...

  2. bzoj1143/2718 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  3. 转 #HTTP协议学习# (一)request 和response 解析

    http://www.cnblogs.com/bukudekong/p/3834020.html #HTTP协议学习# (一)request 和response 解析   注:本文转自:http:// ...

  4. 开发使用mysql的一些必备知识点整理(三)高级

    简介 实体与实体之间有3种对应关系,这些关系也需要存储下来 在开发中需要对存储的数据进行一些处理,用到内置的一些函数 视图用于完成查询语句的封装 事务可以保证复杂的增删改操作有效 关系 创建成绩表sc ...

  5. hdu4183往返经过至多每个点一次/最大流

    题意:从s到t,每个点有f值,只能从f值小的到大的,到T后回来,只能从f值大的到 小的,求可行否. 往返,其实就是俩条路过去(每个点最多一次),所以想到流量为2,跑最大流,看是否满2,又要每个点最多一 ...

  6. Django-自己写的py文件调用models&Non-ASCII character报错&url接收参数

    1.这个设置是网上能查到的最多的,但是没解决我的问题: Django的models.py在外部独立使用,新建一个文件夹,和monitor1目录平级 import sys,os sys.path.app ...

  7. Codeforces Gym 100203E bits-Equalizer 贪心

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...

  8. Visual Studio各版本下载链接

    Visual Studio 2015 企业版 链接: https://pan.baidu.com/s/1NFGyIbm2RNsuwwEU-bYicQ 提取码: m961 Visual Studio 2 ...

  9. python导入sklearn模块出现DLL load failed的解决办法

    笔者安装的python版本是2.7.6,最近在导入sklearn(版本:0.16.1)的模块时,经常出现DLL load failed的报错,具体截图如下: 解决办法与步骤如下: 由于sklearn的 ...

  10. cookie理解

    cookie的作用域是domain本身以及domain下的所有子域名. cookie的作用域是domain本身以及domain下的所有子域名. cookie的作用域是domain本身以及domain下 ...