struts2自定义拦截器 设置session并跳转
实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆。
为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟
修改web.xml
view plaincopy to clipboardprint?
<session-config>
<session-timeout>1</session-timeout>
</session-config>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
此实例用到的jsp页面及登陆所涉及到的相关代码请参考:
Struts2自定义拦截器实例—登陆权限验证
实现自定义拦截器类
view plaincopy to clipboardprint?
package com.ywjava.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.ywjava.action.LoginAction;
import com.ywjava.utils.Constants;
public class SessionIterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
Action action = (Action) actionInvocation.getAction();
if (action instanceof LoginAction) {
return actionInvocation.invoke();
}
String userName = (String) session.get(Constants.USER_SESSION);
if (userName == null) {
return Action.LOGIN;
} else {
return actionInvocation.invoke();
}
}
}
package com.ywjava.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.ywjava.action.LoginAction;
import com.ywjava.utils.Constants;
public class SessionIterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
Action action = (Action) actionInvocation.getAction();
if (action instanceof LoginAction) {
return actionInvocation.invoke();
}
String userName = (String) session.get(Constants.USER_SESSION);
if (userName == null) {
return Action.LOGIN;
} else {
return actionInvocation.invoke();
}
}
}
struts.xml中定义并使用此拦截器
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="authority" extends="struts-default">
<!-- 定义一个拦截器 -->
<interceptors>
<interceptor name="authority"
class="com.ywjava.interceptor.LoginInterceptor">
</interceptor>
<interceptor name="sessionout"
class="com.ywjava.interceptor.SessionIterceptor"></interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
<interceptor-ref name="sessionout"/>
</interceptor-stack>
</interceptors>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login">/login.jsp</result>
</global-results>
<action name="loginform"
class="com.ywjava.action.LoginFormAction">
<result name="success">/login.jsp</result>
</action>
<action name="login" class="com.ywjava.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="show" class="com.ywjava.action.ShowAction">
<result name="success">/show.jsp</result>
<!-- 使用此拦截器 -->
<interceptor-ref name="mydefault" />
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="authority" extends="struts-default">
<!-- 定义一个拦截器 -->
<interceptors>
<interceptor name="authority"
class="com.ywjava.interceptor.LoginInterceptor">
</interceptor>
<interceptor name="sessionout"
class="com.ywjava.interceptor.SessionIterceptor"></interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
<interceptor-ref name="sessionout"/>
</interceptor-stack>
</interceptors>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login">/login.jsp</result>
</global-results>
<action name="loginform"
class="com.ywjava.action.LoginFormAction">
<result name="success">/login.jsp</result>
</action>
<action name="login" class="com.ywjava.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="show" class="com.ywjava.action.ShowAction">
<result name="success">/show.jsp</result>
<!-- 使用此拦截器 -->
<interceptor-ref name="mydefault" />
</action>
</package>
</struts>
当我们登陆后一分钟不做任何操作刷新后则会跳转到登陆页面
struts2自定义拦截器 设置session并跳转的更多相关文章
- Struts2透过自定义拦截器实现登录之后跳转到原页面
Struts2通过自定义拦截器实现登录之后跳转到原页面 这个功能对用户体验来说是非常重要的.实现起来其实很简单. 拦截器的代码如下: package go.derek.advice; import g ...
- Struts2自定义拦截器Interceptor以及拦截器登录实例
1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...
- Struts2 自定义拦截器
自定义拦截器(权限管理),包含了对ajax和表单请求的拦截 package com.interceptor; import java.io.IOException; import java.io.Pr ...
- struts2自定义拦截器与cookie整合实现用户免重复登入
目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...
- Struts2自定义拦截器处理全局异常
今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...
- Struts2自定义拦截器——完整实例代码
比如一个网上论坛过滤系统,将网友发表的不文明.不和谐的语言,通过拦截器对这些文字进行自动替代. 该项目包含: 1.自定义拦截器(MyInterceptor.java) 2.发表评论的页面(news.j ...
- 12.Struts2自定义拦截器
12.自定义拦截器 拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...
- Struts2自定义拦截器
1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...
随机推荐
- AngularJS之ng-class
https://www.cnblogs.com/CreateMyself/p/5566412.html
- extract specified contents from two files.
src_dir=$(pwd)/All_h dst_dir=$(pwd)/All diff_dir=$(pwd)/diff if [ ! -d $diff_dir ] then mkdir $diff_ ...
- shell 从函数文件中调用函数的方法
你可以把所有的函数存储在一个函数文件中 你可以把所有的文件函数加载到当前脚本或命令行 加载函数文件中所有函数的方法: source xxx.sh
- XStream教程
XStream是一个简单的基于Java库,Java对象序列化到XML,反之亦然(即:可以轻易的将Java对象和xml文档相互转换). 特点 使用方便 - XStream的API提供了一个高层次外观,以 ...
- html5中利用FileReader来读取文件。
利用FileReader来读取文件的能够来实现即时预览的效果,这个也是在html5中才有的功能. <!DOCTYPE html> <html lang="en"& ...
- Apache和Tomcat的区别是什么?
Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是web服 ...
- Spring入门之生命周期
好几日没有读东西了,今天本来要读mybatis原理部分,但是看到作者讲,只是学会用不用学那么深,遂直接开干spring,工作中一直用springboot,框架都是领导搭好的,每天的任务就是增删改查,挺 ...
- JSP简单练习-一个简单的计数器
在JSP中,在"<%"和"%>"之间书写的程序代码成为java程序片. 一个JSP页面中能够有多个java程序片. 要注意的是,在Java程序片中声 ...
- 停止node进程
运行vue-cli项目的时候经常出现端口号占用,npm run dev报错的信息, 此时可通过任务管理器粗暴的杀死node进程,也可以通过cmd检测占用某个端口的程序,进而杀死该进程,步骤如下: 1. ...
- Eclipse中发布Maven管理的Web项目时找不到类的问题根源和解决办法(转)
转自:http://blog.csdn.net/lvguanming/article/details/37812579?locationNum=12 写在前面的话 现在是越来越太原讨厌Eclipse这 ...