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 ...
随机推荐
- PicoCTF 2013 Dark Star 分析
0x00题目 题目可以从GitHub中找到:https://github.com/picoCTF/2013-Problems/blob/master/Dark%20Star/darkstar.img ...
- 18. HTTP协议一:概述、原理、版本、请求方法
HTTP协议概述 HTTP协议就是我们常说的超文本协议(HyperText Transfer Protocol).HTTP协议是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准. ...
- bootstrap Grid布局(网格布局)
基本网络结构 <div class="container"> <div class="row"> <div class=" ...
- shell编程:find命令
写在前面 在linux的日常管理中,find的使用频率很高,熟练掌握对提高工作效率很有帮助. find的语法比较简单,常用参数的就那么几个,比如-name.-type.-ctime等.初学的同学直接看 ...
- pip安装Scrapy因网络问题出错备选方案
一 更改pypi默认源 执行 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 执行pip instal ...
- Python升级3.多
本文主要介绍在Linux(CentOS)下将Python的版本升级为3.6.2的方法 众所周知,在2020年python官方将不再支持2.7版本的python,所以使用3.x版本的python是必要的 ...
- 從nasm assembly看函數參數傳遞
在淘宝定了<<C++程序设计语言(特别版)>> 后天才能到货.从网上下了<<C++ Primer中文版>>的电子书看看.找找C++的感觉先. 先看看基本 ...
- pthread_create()的一个错误示例
//pthread_create()函数的错误示例 //新建线程同时传入线程号.线程号总和和消息 #include <stdio.h> #include <pthread.h> ...
- change transformation file in PI interface
1. Jane extends the ZTMMASKU sap table 2. Jane write the program to write the new attribute to the t ...
- Spark Core 1.3.1源码解析及个人总结
本篇源码基于赵星对Spark 1.3.1解析进行整理.话说,我不认为我这下文源码的排版很好,不能适应的还是看总结吧. 虽然1.3.1有点老了,但对于standalone模式下的Master.Worke ...