一、了解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. css的部分应用示例

    CSS :层叠样式表,Cascading Style Sheets.CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化. 1 vertical-align 在图片与文字对 ...

  2. Django ConnectionAbortedError WinError 10053 错误

    因为ajax默认是异步提交,可是有时候我们会发现,本来要求请求马上出现,可是异步会导致后面突然再执行,这样就出问题了. (1)添加这样一段代码 $.ajaxSetup({ async : false ...

  3. 标准C程序设计七---121

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  4. LeetCode OJ--Triangle **

    https://oj.leetcode.com/problems/triangle/ 一个三角形,类似于杨辉三角的形状,求从上到下最小的路径和,但走每一步都得是相邻的. 动态规划,从下到上一层层来. ...

  5. R语言实战读书笔记(七)基本统计分析

    summary() sapply(x,fun,options):对数据框或矩阵中的每一个向量进行统计 mean sd:标准差 var:方差 min: max: median: length: rang ...

  6. SRM1154--Topcoder初体验

    SRM 711 DIV2 <br > 在frank_c1的帮助下,辣鸡Xiejiadong也开始做Topcoder辣...... <br > 这算是一次Topcoder的初体验 ...

  7. Java Web工程连接MySQL数据库及Tomcat服务器页面中文乱码

    Java Web工程连接MySQL数据库 一. 准备工作 1.下载连接MySQL数据库的JDBC (可以去官网下,也可以去百度云找) 2.将下载的jar文件复制到Tomcat的lib目录下 3.新建一 ...

  8. IIS下安装memcached管理工具—MemAdmin

    1.先看这篇文章 http://www.cnblogs.com/joylee/archive/2013/01/07/memadmin.html . 2.在IIS下安装的php-cgi.exe程序版本为 ...

  9. angular md-toast 颜色

    How to show md-toast with background color https://codepen.io/neilkalman/pen/jWBqve <div ng-contr ...

  10. linux中ERROR: The partition with /var/lib/mysql is too full!解决的方法

    今天在ubuntu上遇见这个问题.应该是我的第一分区太小了. 解决的方法: bey0nd@wzw:/var$ cd /var bey0nd@wzw:/var$ rm -rf log 我们删除日志文件 ...