十三、struts2使用自定义拦截器
来源:互联网 发布:suse linux设置网络 编辑:程序博客网 时间:2024/05/20 14:26
一、接文章十二在struts-demo.xml中,声明拦截器、定义拦截器栈xml配置如下:
<!-- 声明拦截器 --><interceptors><interceptor name="mytime" class="interceptor.MyTimeInterceptor"></interceptor><!-- 定义拦拦截器栈 --><interceptor-stack name="myinterceptorstack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="mytime"></interceptor-ref></interceptor-stack></interceptors>
整体xml文件为:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="demo" namespace="" extends="base"><!-- 声明拦截器 --><interceptors><interceptor name="mytime" class="interceptor.MyTimeInterceptor"></interceptor><!-- 定义拦拦截器栈 --><interceptor-stack name="myinterceptorstack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="mytime"></interceptor-ref></interceptor-stack></interceptors><action name="date" class="action.DataTypeConvertAction" ><result name="ok">/dateType.jsp</result></action><action name="fileup" class="action.FileupAction" ><result name="ok">/fileup.jsp</result></action><action name="down" class="action.DownAction" ><result name="ok" type="stream"><!--有输出中文的时使用 param name="contentType">application/octet-stream;charset=utf-8</param --> <param name="contentType">application/x-msdownload</param> <param name="inputName">tagFile</param> <param name="contentDisposition">filename=${filename}</param></result></action></package></struts>
注意:声明拦截器配置必须放在xml最前部分,要定义一个拦截器栈并在栈中将struts2的默认拦截器继承下来因为其他功能都是基于默认拦截器实现的二、在fileup action 中引用拦截器栈
<interceptor-ref name="myinterceptorstack"></interceptor-ref>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="demo" namespace="" extends="base"><!-- 声明拦截器 --><interceptors><interceptor name="mytime" class="interceptor.MyTimeInterceptor"></interceptor><!-- 定义拦拦截器栈 --><interceptor-stack name="myinterceptorstack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="mytime"></interceptor-ref></interceptor-stack></interceptors><action name="date" class="action.DataTypeConvertAction" ><result name="ok">/dateType.jsp</result></action><action name="fileup" class="action.FileupAction" ><interceptor-ref name="myinterceptorstack"></interceptor-ref><result name="ok">/fileup.jsp</result></action><action name="down" class="action.DownAction" ><result name="ok" type="stream"><!--有输出中文的时使用 param name="contentType">application/octet-stream;charset=utf-8</param --> <param name="contentType">application/x-msdownload</param> <param name="inputName">tagFile</param> <param name="contentDisposition">filename=${filename}</param></result></action></package></struts>
三、根据xml配置 建立拦截器实现类MyTimeInterceptor.java 该类实现Interceptor接口 代码如下:
package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class MyTimeInterceptor implements Interceptor {@Overridepublic void destroy() {System.out.println("自定义拦截器销毁!");}@Overridepublic void init() {System.out.println("自定义拦截器初始化!");}@Overridepublic String intercept(ActionInvocation invocation) throws Exception {long star = System.currentTimeMillis();String str = invocation.invoke();// 接着调用后续的拦截器或是ACTIONlong end = System.currentTimeMillis();System.out.println("拦截器执行时间:" + (end - star));return str;}}项目结构及运行结果如下图:
完成!
0 0
- 十三、struts2使用自定义拦截器
- struts2--使用自定义拦截器
- Struts2基础之十三:Struts2拦截器
- Struts2 自定义拦截器配置和使用
- struts2自定义拦截器的使用
- Struts2使用14 -- 拦截器4 -- 自定义拦截器
- 框架技术--struts2拦截器与自定义拦截器使用
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- struts2自定义拦截器
- 无法访问windows installer服务
- SQL Server 2012笔记分享-4:理解SQL server实例
- 测试
- 派生类的构造函数与析构函数
- Android.mk编译带c++ stl的程序
- 十三、struts2使用自定义拦截器
- 自定义AlertDialog
- ThinkPhp3.2 无法加载模块:Index
- linux install go & gogs &git
- Android项目实践之菜鸟养成 (三)Android简单页面跳转
- Spring Boot中使用JdbcTemplate访问数据库
- 4sum
- python爬取廖雪峰javascript教程
- tolua注册对象到lua,并在lua中调用的实现