Struts2拦截器 解决登录问题
一、了解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拦截器 解决登录问题的更多相关文章
- 基于SSH2框架Struts2拦截器的登录验证实现(转)
大象在这里假设你已经弄清楚了Struts2拦截器的基本概念,可以进入实际运用了.那么我们在之前的基础上只需要做下小小的改变,就可以使用Struts2的拦截器机制实现登录的验证. 修改数 ...
- Struts2透过自定义拦截器实现登录之后跳转到原页面
Struts2通过自定义拦截器实现登录之后跳转到原页面 这个功能对用户体验来说是非常重要的.实现起来其实很简单. 拦截器的代码如下: package go.derek.advice; import g ...
- Struts2拦截器登录验证
Struts2拦截器 Struts2拦截器的概念和Spring Mvc拦截器一样. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截 ...
- 防止未登录用户操作—struts2拦截器简单实现(转)
原文地址:http://blog.csdn.net/zhutulang/article/details/38351629 尊重原创,请访问原地址 一般,我们的web应用都是只有在用户登录之后才允许操作 ...
- struts2(五)之struts2拦截器与自定义拦截器
前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...
- struts2 拦截器
拦截器:对Action的访问.可以拦截到Action中某个方法.与过滤器不同,过滤器过滤的是请求.过滤JSP.html.但是拦截器不能拦截jsp.html的访问. Struts2 拦截器在访问某个 A ...
- 浅谈Struts2拦截器的原理与实现
拦截器与过滤器 拦截器是对调用的Action起作用,它提供了一种机制可以使开发者定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行.同时也是提供了 ...
- (三)Struts2 拦截器
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:拦截器简介 (百度百科Struts2) Struts2 拦截器是在访问某 ...
- Struts2拦截器总结<转>
由于项目中在登录跳转到其他应用程序模块的时候有用到拦截器,因此查看了一下相关资料. 原文地址:http://blog.csdn.net/sendfeng/article/details/4248120 ...
随机推荐
- P1382 楼房 (扫描线,线段树)
题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平线高度为0.在轮廓 ...
- Windows connect to mysql failed: can't get hostname for your address
My mysql is on Linux platform. When I used my laptop connect to mysql, I got error message like &quo ...
- CodeforcesD. Aztec Catacombs
$n \leq 300000$的完全无向图,每条边有可行和不可行的状态,一开始只有$m \leq 300000$条边是可行的,给出.每次从$x$走到$y$时,所有与$x$相连的边的可行/不可行状态会改 ...
- 标准C程序设计七---115
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- centos 7安装golang1.10
一.安装&配置 官方下载包(一般需要梯子) https://golang.org/dl/ wget https://dl.google.com/go/go1.10.linux-amd64.ta ...
- 洛谷——P1078 文化之旅
P1078 文化之旅 题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国家可能 ...
- 「NOI2014」动物园
link : https://loj.ac/problem/2246 水水KMP #include<bits/stdc++.h> #define ll long long #define ...
- Markdown的css样式源码
http://www.cnblogs.com/zhangjk1993/p/5442676.html https://github.com/zhangjikai/markdown-css https:/ ...
- 邁向IT專家成功之路的三十則鐵律 鐵律六:求全求盈之道-佈施
如果您只是在IT方面的專業技術與經驗相當高超,而不懂得在日常生活之中當一位俠義肝膽之人,來隨時隨地伸手幫助身旁需要幫助的人,那麼您只能算是一位有勇有謀但卻無智慧的匹夫罷了.既是匹夫那麼即便成功也會是短 ...
- LibSVM 安装使用
知道这个库已经很长的时间了,一直没有实践,以前也看过svm的理论,今天开始安装一下一直感觉有错误,结果自己傻了,根本没有错,可以直接使用... libsvm参考资料: libsvm下载网址:http: ...