struts2自定义拦截器示例

本文介绍如何在Struts2框架中创建自定义拦截器,包括实现Interceptor接口,重写init()、intercept()和destroy()方法,以及在struts.xml中配置自定义拦截器栈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址为: struts2自定义拦截器示例

自定义拦截器栈


如何自定义拦截器:
  

 * 所有的拦截器都需要实现Interceptor接口或者继承Interceptor接口的扩展实现类

* 要重写init()、intercept()、destroy()方法

* init()是在struts2框架运行时执行,在拦截器的生命周期中只执行一次,可以做必要的内容的初始化工作

* intercept(),是每一次请求就执行一次,做相关处理工作。

* intercept()方法接收一个ActionInvocation接口的实例

* 通过这个接口的实例,可以获取以下内容

//cn.itcast.aop.UserAction@15b5783,动作类的对象
System.out.println("invocation.getAction() : "+invocation.getAction());

//cn.itcast.aop.UserAction@15b5783,与invocation.getAction()方法获取的是同一的对象
System.out.println("invocation.getProxy().getAction() : "+invocation.getProxy().getAction());

//userAction_save,自定义配置文件中的action标签的name属性的值
System.out.println("invocation.getProxy().getActionName() : "+invocation.getProxy().getActionName());

//save,对应动作类指定要执行的方法名
System.out.println("invocation.getProxy().getMethod() : "+invocation.getProxy().getMethod());

// /aop,自定义配置文件中的package标签的namespace属性的值
System.out.println("invocation.getProxy().getNamespace() : "+invocation.getProxy().getNamespace());

* destroy()是在拦截器销毁前执行,在拦截器的声明周期中只执行一次。

* 在struts.xml配置文件中,进行注册
* 在配置文件中的package标签下,进行相关配置:

<interceptors>
<!-- 声明自定义的拦截器 -->
<interceptor name="expessionInterceptor" class="cn.itcast.aop.ExpessionInterceptor" />

<!-- 声明自定义拦截器栈 -->
<interceptor-stack name="expessionStack">
  <!--新的拦截器栈一定要引用defaultStack拦截器栈,否则,struts的定义好的拦截器就没有办法使用了-->
<interceptor-ref name="defaultStack"/>

<!-- 配置使用自定义拦截器 -->
<interceptor-ref name="expessionInterceptor"/>

</interceptor-stack>
</interceptors>

<!-- 配置修改struts2框架运行时,默认执行的是自定义拦截器栈 -->
<default-interceptor-ref name="expessionStack" />


    
    
    创建拦截器类,该拦截器实现的功能为当jsp页面访问一个action类时,

    判断session是否已经存在user,若是则继续执行其后的action,若否则跳转到制定页面

public class ExpessionInterceptor implements Interceptor {

public void init() {
System.out.println("ExpessionInterceptor ********* init()");

}

public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("ExpessionInterceptor ********* intercept()");

//cn.itcast.aop.UserAction@15b5783,动作类的对象
System.out.println("invocation.getAction() : "+invocation.getAction());

//cn.itcast.aop.UserAction@15b5783,与invocation.getAction()方法获取的是同一的对象
System.out.println("invocation.getProxy().getAction() : "+invocation.getProxy().getAction());

//userAction_save,自定义配置文件中的action标签的name属性的值
System.out.println("invocation.getProxy().getActionName() : "+invocation.getProxy().getActionName());

//save,对应动作类指定要执行的方法名
System.out.println("invocation.getProxy().getMethod() : "+invocation.getProxy().getMethod());

// /aop,自定义配置文件中的package标签的namespace属性的值
System.out.println("invocation.getProxy().getNamespace() : "+invocation.getProxy().getNamespace());

//null 返回结果
System.out.println("invocation.getResult() : "+invocation.getResult());

Map sessionMap = ServletActionContext.getContext().getSession();

Object obj = sessionMap.get("user");

if(obj==null||obj.equals("")){
return "error";
}else{
return "success";
}
}

public void destroy() {
System.out.println("ExpessionInterceptor ********* destroy()");

}

}


xml配置文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"	"http://struts.apache.org/dtds/struts-2.3.dtd"><struts>	<package name="aop" namespace="/aop" extends="struts-default">				<interceptors>			<!-- 声明自定义的拦截器 -->			<interceptor name="expessionInterceptor" class="ExpessionInterceptor" />						<!-- 声明自定义拦截器栈 -->			<interceptor-stack name="expessionStack">                <interceptor-ref name="defaultStack"/>                                <!-- 配置使用自定义拦截器 -->                <interceptor-ref name="expessionInterceptor"/>                            </interceptor-stack>		</interceptors>				<!-- 配置修改struts2框架运行时,默认执行的是自定义拦截器栈 -->		<default-interceptor-ref name="expessionStack" />				<action name="userAction_save" class="UserAction" method="save">			<result name="success">/aop/success.jsp</result>			<result name="error">/aop/error.jsp</result>		</action>	</package></struts>

包结构

jsp页面结构



转载请注明本文地址: struts2自定义拦截器示例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值