Struts2的Session超时返回登录页面

本文详细介绍了如何在S2SM项目中通过修改web.xml、自定义登录超时拦截器、配置struts.xml以及创建特定页面来实现在session超时或失效时自动跳转到登录页面重新登陆的功能。利用EasyUI框架,实现了这一功能,确保用户体验不受影响。

今天做一个S2SM的项目时,在session超时或者失效时返回到登陆页面重新登陆,前台使用的easyui框架

1、首先修改web.xml

  <session-config>

   <session-timeout>1</session-timeout>

  </session-config>

2、自定义一个登录超时的拦截器

package com.tungkong.util;

import java.util.Hashtable;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

 

@SuppressWarnings("serial")

public class LoginedCheckInterceptor extends AbstractInterceptor {

/** session过期、登录有效性及操作的权限验证拦截器 */

@SuppressWarnings("unchecked")

@Override

public String intercept(ActionInvocation ai) throws Exception {

  //取得请求的URL  

String url = ServletActionContext.getRequest().getRequestURL().toString();

HttpServletResponse response = ServletActionContext.getResponse();

response.setHeader("Pragma""No-cache");

response.setDateHeader("Expires", 0);

response.setHeader("Cache-Control""no-cache");

response.setHeader("Cache-Control""no-store");

response.setHeader("Content-Type""text/html; charset=UTF-8");

response.setHeader("Cache-Control""no-store");

response.setHeader("Content-Type""text/html; charset=UTF-8");

Hashtable<String, Object> userinfo = null;

//对登录与注销请求直接放行,不予拦截

if (url.indexOf("loginAction") != -1 || url.indexOf("logoutAction") != -1) {

return ai.invoke();

else {

//验证Session是否过期

if (!ServletActionContext.getRequest().isRequestedSessionIdValid()) {

//session过期,转向session过期提示页,最终跳转至登录页面

return "tologin";

else {

//验证是否已经登录

userinfo = (Hashtable<String, Object>) ServletActionContext.getRequest().getSession().getAttribute("userSessionHt");

if (userinfo == null) {

  //尚未登录,跳转至登录页面

return "tologin";

else {

return ai.invoke();

}

}

}

}

}

 

3、在struts.xml中配置拦截器,并声明一个全局的result,当session失效的时候拦截器会转发到登陆页面,由于我使用的是多个struts文件,故应该这么设置

<?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>

<!-- 加载默认的 struts2 配置文件 -->

 <include file="struts-default.xml" />

 <!-- 业务模块配置 -->

<include file="struts-userinfo.xml" />

……………… 

<constant name="struts.i18n.encoding" value="UTF-8" />

<constant name="struts.objectFactory" value="spring"></constant>

    

<package name="tksm" extends="struts-default">

<interceptors>

 <interceptor name="loginedCheck" class="com.tungkong.util.LoginedCheckInterceptor" />

 <interceptor-stack name="mystack">

  <interceptor-ref name="loginedCheck" />

  <interceptor-ref name="defaultStack" />

</interceptor-stack>

</interceptors>

 <global-results>

<result name="exception">/exception.jsp</result>

<result name="tologin">/relogin.jsp</result>

</global-results>

<global-exception-mappings>

<exception-mapping exception="java.lang.Exception" result="exception" />

</global-exception-mappings>    

</package>

</struts>  

 

4、在struts-userinfo.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>

 

<package name="tksm-user" extends="tksm">

<default-interceptor-ref name="mystack" />

 

        <action name="loginAction" class="userAction" method="login">

            <result name="success">/WEB-INF/index/main.jsp</result>

            <result name="error">/WEB-INF/index/error.jsp</result>

        </action>

……………………

    </package>

</struts> 

 

5、新建relogin.jsp页面

由于页面嵌套在iframe下,跳转时需要跳转到其父页面,因此加个中间的jsp,拦截器配置跳转到此页面,再由此页面跳转到登录页面。

relogin.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

  </head>

  

  <body>

    <script type="text/javascript">

     alert("对不起!您长时间对系统未进行操作,登录已超时,请重新登录!");

     window.top.location.href="<%=basePath%>login.jsp";

    </script>

  </body>

</html>

当我们登陆后一分钟不做任何操作刷新后则会跳转到登陆页面

这篇博客主要来源于:

http://blog.youkuaiyun.com/java_cxrs/article/details/5519743

http://blog.sina.com.cn/s/blog_a72f208a01014gha.html

感谢两位!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值