---
title: "自定义Interceptor后获得Session对象"
author: "ztfjava"
date: "2010-09-13T20:10:11Z"
tags: []
categories: ["企业架构"]
slug: "interceptor-session"
draft: false
lastmod: "2010-09-13T20:10:11Z"
id: 762396
url: "https://ztfjava.iteye.com/blog/762396"
---

自定义拦截器的时候 主要分为 三步

1.定义拦截器类 重要的是要继承 AbstractInterceptro 类 重写里面的 intercept(ActionInvocation   actionInvocation) 方法

　　　　另外注意  其中得到session 对象的方法

　　　　ActionContext  ac = actionInvocation.getInvocationContext();

　　　　Map<String,Object> session = ac .getSession();

2.在struts2 的配置文件中 struts2.xml  文件中声明配置 intercepor

<package name="default" extends="struts-default" namespace="/">

　　　　<!--声明拦截器-->

　　　　<interceptor name="LoginInterceptor" class="com.ztf.action.LoginInterceptor"/>

　　　　<!--当拦截器 将请求拦截后 处理失败 将会跳转到指定的页面 

　　　　不再继续执行Action 这是要定义一个全局的 results-->

　　　　<global-results>

　　　　　　　　<result name="login">/login.jsp</result>

　　　　</global-results>

　　　　<!--在Action中调用拦截器-->

　　　　<action name="UserAction" class="com.ztf.action.UserAction">

　　　　　　　<!--在Action中引用拦截器-->

　　　　　　　　<intercepor-ref>LoginInterceptor</interceptor-ref>

　　　　　　　　<result name="success">/showUser.jsp</result>

　　　　</action>

</package>