如下文章節選自《Netkiller Java 手札》 html
一下文章節選擇《netkiller java 手札》 java
http://www.netkiller.cn/java/struts/interceptor.html web
在 web.xml 文件中定義 Session 超時時間 session
<session-config> <session-timeout>30</session-timeout> </session-config>
建立攔截器程序 jsp
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(); } }
配置攔截器 ide
<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>