struts2自定义拦截器二——模拟session超时的处理

实例功能:当用户登陆成功后,若session超时则返回到登陆页面重新登陆。

1、http://localhost:8083/struts2/user.jsp  用户登陆

2、http://localhost:8083/struts2/login/loginHelloWorld.do  若登陆成功,会跳转到成功页面,否则会跳转到session失效提示页面

 配置session失效时间,修改web.xml

 
  
<!-- 设置session失效时间,这里我们设置成1分钟后失效 -->
< session - config >
< session - timeout > 1 </ session - timeout >
</ session - config >

           

登陆页面

 
  
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<%
request.getSession().setAttribute(
" user " , " 林计钦 " );
%>

用户已经登录

              

Action类

 
  
package com.ljq.action;


public class HelloWorldAction {

public String login() {
return " success " ;
}
}

              

session拦截器

 
  
package com.ljq.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
* 自定义session拦截器,30s后失效
*
*
@author jiqinlin
*
*/
@SuppressWarnings(
" serial " )
public class SessionIterceptor extends AbstractInterceptor{

@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ctx
= invocation.getInvocationContext();
String user
= (String)ctx.getSession().get( " user " );
if (user != null && user.equals( " 林计钦 " )){
return invocation.invoke();
}
// 如果超时,返回提示页面
return " index " ;
}



}

            

struts.xml配置文件

 
  
<? xml version = " 1.0 " encoding = " UTF-8 " ?>
<! DOCTYPE struts PUBLIC
" -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
" http://struts.apache.org/dtds/struts-2.0.dtd " >

< struts >
< constant name = " struts.i18n.encoding " value = " UTF-8 " />
< constant name = " struts.enable.DynamicMethodInvocation " value = " false " />
< constant name = " struts.action.extension " value = " do " />

< package name = " login " namespace = " /login " extends = " struts-default " >
< interceptors >
<!-- 自定义拦截器 -->
< interceptor name = " permission " class = " com.ljq.interceptor.SessionIterceptor " />
<!-- 配制默许的拦截器到拦截器栈 -->
< interceptor - stack name = " permissionStack " >
<!-- 此处先后顺序好像没影响哦,我尝试过是没影响的 -->
< interceptor - ref name = " permission " />
< interceptor - ref name = " defaultStack " />
</ interceptor - stack >
</ interceptors >
<!-- 配置默认的拦截器 -->
< default - interceptor - ref name = " permissionStack " />
<!-- 全局变量 -->
< global - results >
< result name = " success " >/ WEB - INF / page / message.jsp </ result >
</ global - results >
< action name = " *HelloWorld " class = " com.ljq.action.HelloWorldAction " method = " {1} " >
<!-- 超时返回登录页面 -->
< result name = " index " >/ index.jsp </ result >
</ action >
</ package >

</ struts >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值