Struts2—拦截器之计算action调用时间
来源:互联网 发布:windows键没反应 win10 编辑:程序博客网 时间:2024/06/05 02:35
在上一篇文章中,记录了拦截器的使用方式有2种。这次学习的内容便是使用继承abstractinterceptor类来实现拦截器的简单应用。
首先我们创建2个jsp页面:timestart.jsp
<body> <a href="timer">点击此处开始计算Action时间</a> </body>timeend.jsp
<body> action执行完毕<br> </body>然后我们来创建一个action:timeaction.java,我们等下通过拦截器来计算这个action的调用时间,所以我们在这个action里面写一个循环。
public class TimerAction extends ActionSupport {@Overridepublic String execute() throws Exception {for(int i=0;i<1000;i++){System.out.println(i);}return SUCCESS;}}在我们之前的xml文件里面配置这个action:
<action name="timer" class="com.imooc.action.TimerAction"> <result>timeend.jsp</result> </action>此时执行一下,可以看到是可以跳转到timeend.jsp的页面。这里就不传结果图了。
接下来制作一个拦截器,首先是继承abstractinterceptor类:TimeInterceptor.java
public class TimeInterceptor extends AbstractInterceptor {@Overridepublic String intercept(ActionInvocation invocation) throws Exception {// 1、执行action之前long start=System.currentTimeMillis();//取当前时间,单位为毫秒//2、执行下一个拦截器,如果是最后一个,执行actionString result=invocation.invoke();//3、action执行之后long end=System.currentTimeMillis();System.out.println("action执行的时间为:"+(end-start));return result;}}
通过以上代码,写好了一个拦截器,下面需要把这个拦截器配置到xml里面: <!--注册拦截器 --> <interceptors> <interceptor name="mytimer" class="com.imooc.interceptor.TimeInterceptor"></interceptor> </interceptors>
<action name="timer" class="com.imooc.action.TimerAction"> <result>timeend.jsp</result> <!--引用拦截器 --> <interceptor-ref name="mytimer"></interceptor-ref> </action>以下是运行结果:
最后可以总结为,拦截器的使用需要2点:
一、继承abstractinterceptor类来创建自定义的拦截器
二、在xml里面配置相应的拦截器和调用。
1 0
- Struts2—拦截器之计算action调用时间
- struts2实现简单action时间拦截器
- Struts2 中拦截器和Action的调用关系
- Struts2 中拦截器和Action的调用关系
- Struts2拦截器获取调用步骤及Action类
- struts2拦截器实现Action调用后逻辑处理
- Struts2 中拦截器和Action的调用关系
- struts2拦截器的方法过滤功能在Action动态方法调用中的应用
- struts2之拦截器
- struts2 之 拦截器
- Struts2之拦截器
- Struts2之拦截器
- struts2之拦截器
- Struts2之拦截器
- Struts2之拦截器
- Struts2之拦截器
- struts2之拦截器
- Struts2之拦截器
- 处理缓存响应头与http-equiv的区别
- 通过React Native动态更新iOS应用
- angular中$watch的简单使用
- tesseract-ocr 实现图片识别功能
- linux下UDP客户端通用发送与接受
- Struts2—拦截器之计算action调用时间
- 删除数字vba
- 最清晰的Android多屏幕适配方案
- AFnetWorking的一个bug
- C++编程入门系列之四十九(多态性:纯虚函数和抽象类)
- HTTP缓存
- 图像的载入、显示和输出示例
- 关于Template
- mysql-5.7.16-winx64.zip安装教程