四:Struts拦截器简单介绍
来源:互联网 发布:广州恒大淘宝队微博 编辑:程序博客网 时间:2024/05/16 12:58
一:简单拦截器
定义拦截器:
public class MyInterceptor extends AbstractInterceptor{public String intercept(ActionInvocation arg0) throws Exception{System.out.println("action调用前");String result=arg0.invoke();//调用下一个拦截器System.out.println("action调用后");return result;}}
*上面是一个简单的拦截器定义,首先继承AbstractInterceptor类,然后重写里面的intercept方法。invoke()方法意思是调用下一个拦截器(如果有)。
配置拦截器:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="default" extends="struts-default"> <interceptors> <!-- 声明拦截器 --> <interceptor name="myInterceptor" class="org.interceptor.MyInterceptor"></interceptor> <interceptor-stack name="myDefaultInterceptor"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <action name="myLogin" class="org.action.LoginAction" method="execute"> <!-- 在action中引用拦截器 --><interceptor-ref name="myInterceptor"></interceptor-ref></action></package></struts>
*上面是对前面定义的拦截器的配置,<interceptor-stack name="myDefaultInterceptor"></interceptor-stack>为拦截器栈,可以容纳多个拦截器,执行顺序为从上到下。
*当为一个action配置拦截器时,默认的拦截器就不起作用了,这时需要显式的配置"defaultStack"这个拦截器栈。
*配置默认拦截器的方法:<default-interceptor-ref name=""></default-interceptor-ref>
action:
public class LoginAction extends ActionSupport{public String execute() throws Exception{System.out.println("action执行中");return NONE;}}
***执行上面代码的结果为:
action调用前
action执行中
action调用后
***
原因:在执行action中的 execute() 方法之前,拦截器拦截,执行下面这段代码:
System.out.println("action调用前");
String result=arg0.invoke();//调用下一个拦截器
没有下一个拦截器,直接运行 execute(),运行完之后回头再执行下面这段代码:
System.out.println("action调用后");
return result;
拦截器,不只是在执行execute()方法之前要执行,而且在execute()方法之后也要执行
二:方法过滤拦截器
**该拦截器可以过滤方法,指定哪些方法要被拦截,哪些不要被拦截。
定义拦截器
public class MyInterceptor extends MethodFilterInterceptor{public String doIntercept(ActionInvocation arg0) throws Exception{System.out.println("action调用前");String result=arg0.invoke();System.out.println("action调用后");return result;}}
*继承MethodFilterInterceptor类,重写doIntercept() 方法
配置拦截器
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="default" extends="struts-default"> <!-- 声明拦截器 --><interceptors><interceptor name="myInterceptor" class="org.interceptor.MyInterceptor"></interceptor><interceptor-stack name="myDefaultInterceptor"><interceptor-ref name="myInterceptor"><param name="includeMethods">execute</param> </interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors><action name="myLogin" class="org.action.LoginAction" method="execute"> <!-- 引用拦截器 --><interceptor-ref name="myInterceptor"></interceptor-ref></action></package></struts>
<param name="includeMethods">method1,method2</param>指定拦截哪些方法
<param name="excludeMethods">method1,method2</param>指定不拦截哪些方法
Struts完整介绍:Struts2拦截器总结
2 0
- 四:Struts拦截器简单介绍
- 拦截 Struts(四)
- Struts学习总结(四)---简单模拟实现Struts2的拦截器
- struts2拦截器简单介绍
- struts拦截器的使用(简单例子)
- 简单的interceptor(拦截器)介绍
- 过滤器 拦截器 监听器 简单介绍
- 简单的interceptor(拦截器)介绍
- Struts拦截器简单实用--用户是否有效
- Struts拦截器-配置及简单应用示例-ssh项目
- Struts 2.0 拦截器
- struts拦截器
- struts 拦截器
- Struts 2拦截器
- Struts拦截器
- Struts 2拦截器
- struts拦截器
- Struts之拦截器
- 20. Valid Parentheses (Easy)
- POJ1251 JungleRoads 最小生成树
- Git客户端下载及安装
- 117. Populating Next Right Pointers in Each Node II
- 聊天机器人--基础版本
- 四:Struts拦截器简单介绍
- 【codevs 1029】遍历问题
- 叙事——2016/11/13
- Android ListView与RecyclerView对比浅析--缓存机制--转自腾讯Bugly 作者 Bugly_Tony
- 第四次修改
- java中嵌入执行shell语句
- java实现循环队列
- 第一个Python单线程爬虫(使用正则表达式)
- Windows下FFTW的安装_史上最全