struts2之自定义拦截器及拦截器生命周期分析
来源:互联网 发布:linux编辑文件命令文件 编辑:程序博客网 时间:2024/06/05 23:03
一,概述
本文主要是关于怎样自定义拦截器及通过自定义的拦截器来了解拦截器的生命周期.
二,自定义拦截器
1)写一个拦截器类HelloInterceptor,它实现了Interceptor接口.
package cn.bighuan.a_interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;/** * 自定义一个拦截器 * * @author bighuan * */public class HelloInterceptor implements Interceptor {// 启动时候执行public HelloInterceptor() {System.out.println("创建了拦截器对象...");}// 启动时候执行@Overridepublic void init() {System.out.println("HelloInterceptor.init():执行了拦截器的初始化方法");}// 拦截器业务处理方法:(在访问Action时候执行?在execute之前执行?)@Overridepublic String intercept(ActionInvocation invocation) throws Exception {System.out.println("2,执行Action之前");// 调用下一个拦截器或Action:相当于Servlet过滤器中的chain.doFilter(...)// 获取的是execute方法的返回值String resultFlag = invocation.invoke();System.out.println("4,拦截器,业务处理-结束:" + resultFlag);return resultFlag;// return null;}@Overridepublic void destroy() {System.out.println("HelloInterceptor.destroy():拦截器销毁了...");}}2)写一个Actio类HelloAction.
package cn.bighuan.a_interceptor;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport {public HelloAction() {System.out.println("1,HelloAction.HelloAction():HelloAction实例创建了");}@Overridepublic String execute() throws Exception {System.out.println("3,HelloAction.execute():执行了请求处理的方法");return super.execute();}}3)在struts.xml文件进行配置,主要要注意拦截器的配置.(步骤在下面代码中)
<package name="hello_interceptor" extends="struts-default"><!-- 拦截器配置 --><interceptors><!-- 配置用户自定义的拦截器 --><interceptor name="helloInterceptor" class="cn.bighuan.a_interceptor.HelloInterceptor"></interceptor><!-- 自定义一个栈:要引用默认栈,否则默认栈功能就没有了,自定义的拦截器 --><interceptor-stack name="helloStack"><!-- 引用默认栈,注意:默认栈一定要放到第一行 --><interceptor-ref name="defaultStack"></interceptor-ref><!-- 引用自定义的拦截器 --><interceptor-ref name="helloInterceptor"></interceptor-ref></interceptor-stack></interceptors><!-- 执行拦截栈中的所有拦截器 --><default-interceptor-ref name="helloStack"></default-interceptor-ref><!-- Action配置 --><action name="hello" class="cn.bighuan.a_interceptor.HelloAction"><result name="success">/index.jsp</result></action></package>4)启动服务器,访问action.
a)刚启动服务器的时候,控制台输出如下内容:
创建了拦截器对象...HelloInterceptor.init():执行了拦截器的初始化方法b)当访问action时,控制台输出如下内容:
1,HelloAction.HelloAction():HelloAction实例创建了2,执行Action之前3,HelloAction.execute():执行了请求处理的方法4,拦截器,业务处理-结束:success
d)当服务器关闭的时候,控制台输出如下:
HelloInterceptor.destroy():拦截器销毁了...
三,分析拦截器的生命周期
1)由上面的实验结果可知,当我们启动服务器时,就已经创建了自定义拦截栈中所有拦截器的对象(如果自己未引用自定义拦截器栈,则执行默认的拦截器栈),并且所有对象都执行了初始化方法init().
2)当用户访问action时,首先是创建Action示例,此时并没有马上执行action对应的业务逻辑方法execute(),而是执行了拦截器中的业务处理方法中invocation.invoke()这行代码之前的代码(包括自己这行代码).invocation.invoke()相当于servlet编程中过滤器的核心业务方法中的chain.doFilter().紧接着,就是先去执行Action中的业务逻辑方法execute(),当执行完execute()之后才会去继续执行拦截器业务处理方法中的invocation.invoke()之后的代码直到方法结束.
3)当服务器关闭,拦截器才会销毁
四,预告
下一篇博客就是通过拦截器来实现一个完整的登录验证,敬请期待吧!
1 0
- struts2之自定义拦截器及拦截器生命周期分析
- Struts2之自定义拦截器
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- Struts2 XML配置详解
- 理解dropout
- java读取.properties配置文件的几种方法
- 各 Android 平台版本支持的 API 级别
- Jmeter接口测试图文示例
- struts2之自定义拦截器及拦截器生命周期分析
- 聊聊高并发系统之队列术
- CREELINKS开源无人机---安装方法
- 淘宝权重及其大数据分析
- 接入ShareSDK第三方登录和分享遇到的问题以及解决方法
- android5.1 自动亮度调节简析
- 一次性重建所有聚集索引,方便释放空间
- Docker之几种storage-driver比较
- 《Cracking the Coding Interview程序员面试金典》----基本字符串压缩