strust2拦截器处理Session超时

 当用户登陆后,session超时后则返回到登陆页面重新登陆。

 

为了方便测试,修改session的有效时间

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

 

 

自己定义一个拦截器

package com.zrwt.sjkd.tools;

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.sun.org.apache.xerces.internal.impl.Constants;
import com.zrwt.sjkd.action.LoginAction;

public class SessionIterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {        
		ActionContext ctx = ActionContext.getContext();   
        Map session = ctx.getSession();   
        Action action = (Action) invocation.getAction();   
        if (action instanceof LoginAction) {  
         //上面的判断是为了过滤登录的Action,如果不判断,登录的Action也会拦截,导致你永远无法登录
            return invocation.invoke();   
        }   
        String userName = (String) session.get("staffId");   
        if (userName == null) {   
            return "index";   
        } else {   
            return invocation.invoke();   
        }   
    }   




}

 

配置struts.xml文件

<!-- 定义一个拦截器 -->   
        <interceptors>   
           
            <interceptor name="sessionout"   class="com.zrwt.sjkd.tools.SessionIterceptor"></interceptor>   
            <!-- 拦截器栈 -->   
            <interceptor-stack name="sjkd">   
                <interceptor-ref name="defaultStack" />   
                <interceptor-ref name="sessionout"/>   
            </interceptor-stack>   
        </interceptors>   
			<default-interceptor-ref name="sjkd" />
//对sjkd包下的所有Action拦截
配置全局变量
<global-results>
				<result name="index">/index.jsp</result>
					</global-results>

  

这样配置过后就会起作用了,等你一分钟不对系统做任何操作,当你操作系统的时候,系统会自动跳转到登录页面

你可能感兴趣的:(apache,jsp,xml,struts,sun)