Struts---拦截器
来源:互联网 发布:linux su认证失败 编辑:程序博客网 时间:2024/06/10 06:10
Struts---拦截器
1、定义
1、定义
Struts2大多数核心功能是通过拦截器实现的,每个拦截器完成某项功能。
拦截器方法在Action执行之前或之后执行。
作用如:数据转移、类型转换、数据校验。自定义拦截器:
有两种方式:
方式一:实现Interceptor接口
有三个方法:
-void init():初始化拦截器所需资源
-void destroy() :释放在init()中分配的资源
-String intercept(ActionInvocation ai)throws Exception
实现拦截器功能
利用ActionInvocation参数获取Action状态
返回result字符串作为逻辑视图
方式二:继承AbstractInterceptor类
-提供了init()和destroy() 方法的空实现
-只需要实现intercept方法即可实际开发当中一般会去继承AbstractInterceptor这个父类
2、实现一个拦截器
@TimerAction.javapublic class TimerAction extends ActionSupport{@Overridepublic String execute() throws Exception {for(int i=0;i<10000;i++){System.out.println("访问suceess.jsp");}return SUCCESS;}}@TimerInterceptor.java/*** 计算执行Action花费的时间*/public class TimerInterceptor extends AbstractInterceptor {@Overridepublic String intercept(ActionInvocation invocation) throws Exception {//1.执行Action之前long start=System.currentTimeMillis();//2.执行下一个拦截器,如果已经是最后一个拦截器,则执行目标Action。返回值:方法中return的值,如"success"。String result=invocation.invoke();//3.执行Action之后long end=System.currentTimeMillis();System.out.println("执行Action花费的时间:"+(end-start)+"ms");System.out.println(result);return result;}}@struts.xml<struts><package name="default" extends="struts-default" namespace="/"><!-- 注册(定义)拦截器 --><interceptors><interceptor name="mytimer" class="com.imooc.interceptor.TimerInterceptor"></interceptor></interceptors><action name="timer" class="com.imooc.action.TimerAction"><result>/success.jsp</result><!-- 引用拦截器 --><interceptor-ref name="mytimer"></interceptor-ref></action></package></struts>@index.jsp<a href="timer">访问TimerAction</a><!-- 因为默认后缀为.action或没有,所以可以不用写timer.action,直接写timer -->提示:实现一个拦截器主要过程:
第一步:实现一个拦截器的类,有两个方法,写一个拦截器的类继承自AbstractInterceptor或者实现Interceptor接口,并且在这个类里面实现对应的intercept方法。
第二步:在struts.xml中对拦截器进行注册(定义),并且在action中对拦截器进行引用。
3、Struts内置拦截器
0 0
- Struts 2.0 拦截器
- struts拦截器
- struts 拦截器
- Struts 2拦截器
- Struts拦截器
- Struts 2拦截器
- struts拦截器
- Struts之拦截器
- struts拦截器
- Struts 拦截器
- struts拦截器
- struts 2 拦截器
- struts 拦截器
- struts自定义拦截器
- struts 拦截器 interceptor
- struts 拦截器总结
- struts拦截器使用方法
- struts 拦截器
- Onekey Ghost找不到硬盘分区怎么办
- 给定日期-判断是星期几
- Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
- WCF服务如何获得客户端IP地址信息
- 数据库中char varchar nchar nvarchar的区别
- Struts---拦截器
- 微商城开发(2)
- 输入与输出
- LIME:一种解释机器学习模型的方法
- git基本操作
- 欧几里得辗转相除求最大公约数最小公倍数
- final和static final之间的区别
- shell学习
- C++实验3—个人所得税计算器