Struts2学习笔记12:Struts2的拦截器【续】
2008-09-23 12:34
477 查看
Struts2学习笔记12:Struts2的拦截器【续】<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
第十一讲
内容很多,零碎的知识点也是比较多的
在此讲中仅仅做一个例子来演示拦截器的应用,所以方法中仅打印一句话。
新建一个Java文件 interceptor.RegisterInterceptor 实现Interceptor接口,代码如下:
说明:解释下invocation.invoke();从API文档中拷贝过来的,英文:
Invokes the next step in processing this ActionInvocation.If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor to execute. If there are no more Interceptors to be applied, the Action is executed. If the ActionProxy getExecuteResult() method returns true, the Result is also
executed.
配置拦截器
打开struts.xml文件
1)在package标签中添加新元素,配置拦截器,用于拦截action
2)在action标签中配置,习惯写于result元素之下。
这里的“自定义”对应于上面的“自定义”字符串
3)运行tomcat,点击按钮,会看到如下网页:
4)分析原因:
关于struts2默认的拦截器
对自定义struts.xml文件中package代码的解释
第十一讲
内容很多,零碎的知识点也是比较多的
用到文件存放位置: struts2-core-2.0.11.2.jar中 com.open.symphony.xwork.interceptor包中的Interceptor类 |
需要实现的方法: void destroy(); void init(); String intercept(ActionInvocation invocation) throws Exception; |
多个过滤器可以组成一个过滤器链 多个拦截器可以组成一个拦截器栈 拦截器与拦截器栈的功能是完全等价的 |
在一个package中只能有一个默认拦截器栈 |
在interceptor中设置param属性值为静态调用 在interceptor-ref中设置param属性值为动态调用(此优先级高) |
新建一个Java文件 interceptor.RegisterInterceptor 实现Interceptor接口,代码如下:
package interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class RegisterInterceptor implements Interceptor { private static final long serialVersionUID = -7376312407476270387L; private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } public void destroy() { System.out.println("destroy"); } public void init() { System.out.println("init"); } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("interceptor"); System.out.println(hello); String result = invocation.invoke(); return result; } } |
Invokes the next step in processing this ActionInvocation.If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor to execute. If there are no more Interceptors to be applied, the Action is executed. If the ActionProxy getExecuteResult() method returns true, the Result is also
executed.
配置拦截器
打开struts.xml文件
1)在package标签中添加新元素,配置拦截器,用于拦截action
<interceptors> <interceptor name="自定义" class="处理类(含包名)"> </interceptor> </interceptors> |
<interceptor-ref name="自定义"></interceptor-ref> |
3)运行tomcat,点击按钮,会看到如下网页:
|
如果一个action手工加入了一个拦截器,则struts2不再将默认的拦截器附加到action 中,如果要附加,在interceptors元素中导入struts2默认的拦截器 |
Struts2-core-2.0.11.2.jar/sturts-default.xml 打开该文件找到一行代码: <interceptor-stack name="defaultStack"> 这里设置的是拦截器栈 通过下面代码将这个拦截器栈设置为默认拦截器栈: <default-interceptor-ref name="defaultStack"/> 要定义自己的默认拦截器栈 1)在interceptors中添加interceptor-stack元素 建议:在自定义的拦截器栈中加入struts2的默认拦截器栈 2)在package中添加default-interceptor-ref元素,name属性值对应于上面 所定义的拦截器栈的值 |
<package name="struts2" extends="struts-default"> 主要是针对 extends="struts-default" 代码 与Java中的继承有些类似,将继承struts-default.xml文件中的所有配置信息 |
在Interceptor和Interceptor-ref元素中有param属性,通过此属性可以设置 拦截器中的字段值
在RegisterInterceptor.java中添加私有字段hello,并设置其get和set方法。private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } 修改intercept方法为:
interceptor world |
如果没有必要用到init和destroy方法,则让自定义的拦截器类,继承 AbstractInteceptor类,只需要重写intercept方法 |
拦截器栈中拦截器的执行顺序: 第一步:先配置的拦截器先实行。 第二步:在返回时,先配置的拦截器后执行。 如下图所示:
|
对于拦截器不仅可以拦截action也可以拦截处理action中的某个方法 需要用到的类:MethodFilterInterceptor 新建RegisterInterceptor3.java 代码如下:
|
相关文章推荐
- struts2系列学习笔记(12)---------------Struts2在OGNL基础上的增强
- Struts2学习笔记---拦截器实例两个
- struts2 学习笔记拦截器
- .Java程序员从笨鸟到菜鸟之(四十七)细谈struts2(九)内置拦截器和自定义拦截器详解(附源码) 分类: 学习专区 框架Struts2 Java程序员从笨鸟到菜鸟 2012-05-08 12:
- Struts2学习笔记之自定义拦截器
- Struts2-学习笔记系列(14)-拦截器
- [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) ModelDriven拦截器 paramter 拦截器
- struts2学习笔记-----拦截器
- struts2系列学习笔记(11)---------------拦截器
- Struts2学习笔记之自定义拦截器
- struts2学习笔记 -- day09 Struts2的拦截器
- struts2学习笔记之拦截器(Interceptor)
- Struts2 学习笔记——struts.xml文件之拦截器的配置
- [原创]java WEB学习笔记70:Struts2 学习之路-- struts2拦截器源码分析,运行流程
- Struts2学习笔记(5)--拦截器
- struts2第十三讲学习笔记,拦截器interceptor的详细讲解
- Struts2 学习笔记18 拦截器原理分析
- struts2学习笔记之拦截器(Interceptor)
- [原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器
- Struts2拦截器Interceptor学习笔记