前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Struts Interceptor Example

Struts Interceptor Example

作者头像
netkiller old
发布2018-03-05 16:13:23
6380
发布2018-03-05 16:13:23
举报
文章被收录于专栏:NetkillerNetkillerNetkiller

一下文章节选择《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>
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 7.6. Interceptor
    • 7.6.1. Session
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档