知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
在 web.xml 檔案中定義 Session 超時時間
<session-config> <session-timeout>30</session-timeout> </session-config>
創建攔截器程序
package cn.netkiller.interceptor; import java.util.Map; import java.lang.Override; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class SessionInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 8347994918002285514L; @Override public String intercept(ActionInvocation invocation) throws Exception { Map<String, Object> session = invocation.getInvocationContext().getSession(); if (session.isEmpty()) return "nosession"; // session is empty/expired return invocation.invoke(); } }
配置攔截器
<package name="mobile" extends="main" namespace="/mobile"> <global-results> <result name="nosession" type="redirectAction"> <param name="actionName">Index</param> <param name="namespace">/mobile</param> </result> </global-results> <interceptor name="session" class="cn.netkiller.SessionInterceptor" /> <interceptor-stack name="sessionExpirayStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="session"/> </interceptor-stack> <default-interceptor-ref name="sessionExpirayStack" /> <action name="testAction" class="TestClass"> <interceptor-ref name="sessionExpirayStack" /> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package>