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>
当我们登陆后一分钟不做任何操作刷新后则会跳转到登陆页面