欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts Interceptor Example

程序员文章站 2022-07-14 13:37:32
...

以下文章节选自《Netkiller Java 手札》

Struts Interceptor Example

原创 2016-04-23 景峯  Netkiller

一下文章节选择《netkiller java 手札》 


http://www.netkiller.cn/java/struts/interceptor.html


7.6. Interceptor

7.6.1. Session

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

Struts Interceptor Example

转载于:https://my.oschina.net/neochen/blog/664892