- 浏览: 153571 次
- 性别:
- 来自: 杭州
文章分类
最新评论
一.Struts2使用拦截器步骤:
1. 定义拦截器类
2. 声明拦截器
3. 使用拦截器
1. 定义拦截器MyInterceptor.java
2. 声明拦截器 struts.xml
3. 使用拦截器 struts.xml
注:拦截器在服务器启动时就加载,并初始化。
二.Struts2.xml中使用拦截器问题
1. 如上使用拦截器会发现输入结果不能被传到结果页面
这是因为strut2.xml继承了struts-default文件,这个XML文件中定义了很多拦截器,且定义了默认使用的拦截器组defaultStack,如果在struts.xml中Aciton使用了自定的拦截器,默认的使用的拦截器无效,解决方法就是在引用一下defaultStack拦截器
三.Struts2.xml中使用拦截器同Filter可以对黑名单进行操作
1. struts.xml
2. MyInterceptor.java
四.为了避免实现Interceptor接口时要实现init(),destory(),只需要interceptor(),可以继承抽象类AbstractInterceptor
1. AbstractInterceptor.java
2. struts.xml
五.Action中的方法拦截器,继承抽象类MethodFilterInterceptor
1. MethodFilterInterceptor.java
2. struts.xml
六.拦截器栈(拦截器的集合)
1. struts.xml
七.自定义默认的拦截器
1. struts.xml
注:自定义默认的拦截器会对所有Action生效,所有一般情况不会用自定默认拦截器
八.在拦截器中增加监听器(观察者模式)
1. 定义监听器MyListener.java实现PreResultListener(在execute方法执行完,result执行之前调用)接口
2. 在拦截器MyInterceptor中增加监听器
对于以上方式,只有一处会用到的一个类的一个简单方法,可以不用自定义监听器,直接在拦截器中用一个匿名类来实现,如下:
九.拦截器应用的场合(最重要的应用是权限验证)
1. 定义权限拦截器AuthInterceptor.java
2. struts.xml增加拦截器
1. 定义拦截器类
2. 声明拦截器
3. 使用拦截器
1. 定义拦截器MyInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { @Override public void destroy() { System.out.println("destroy invoked"); } @Override public void init() { System.out.println("init invoked"); } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; } }
2. 声明拦截器 struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> </action> </package> </struts>
3. 使用拦截器 struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> </action> </package> </struts>
注:拦截器在服务器启动时就加载,并初始化。
二.Struts2.xml中使用拦截器问题
1. 如上使用拦截器会发现输入结果不能被传到结果页面
这是因为strut2.xml继承了struts-default文件,这个XML文件中定义了很多拦截器,且定义了默认使用的拦截器组defaultStack,如果在struts.xml中Aciton使用了自定的拦截器,默认的使用的拦截器无效,解决方法就是在引用一下defaultStack拦截器
<action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action>
三.Struts2.xml中使用拦截器同Filter可以对黑名单进行操作
1. struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
2. MyInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } @Override public void destroy() { System.out.println("destroy invoked"); } @Override public void init() { System.out.println("init invoked"); System.out.println("hello:"+hello); } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; } }
四.为了避免实现Interceptor接口时要实现init(),destory(),只需要interceptor(),可以继承抽象类AbstractInterceptor
1. AbstractInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor2 extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.err.println("MyInterceptor2 invoked"); String result=invocation.invoke(); System.out.println("MyInterceptor2 invoke result:"+result); return result; } }
2. struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
五.Action中的方法拦截器,继承抽象类MethodFilterInterceptor
1. MethodFilterInterceptor.java
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class MyInterceptor3 extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("MyInterceptor3 invoked"); String result=invocation.invoke(); System.out.println("MyInterceptor3 invoke result:"+result); return result; } }
2. struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="myInterceptor3"> <!-- <param name="excludeMethods">execute</param> --> 没有param就拦截所有方法,includeMethods拦截指定方法,excludeMethods不拦截指定方法 </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
六.拦截器栈(拦截器的集合)
1. struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> </interceptor-stack> </interceptors> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <!-- <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> --> <interceptor-ref name="myStack"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
七.自定义默认的拦截器
1. struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <!-- <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="myStack"></interceptor-ref> --> </action> </package> </struts>
注:自定义默认的拦截器会对所有Action生效,所有一般情况不会用自定默认拦截器
八.在拦截器中增加监听器(观察者模式)
1. 定义监听器MyListener.java实现PreResultListener(在execute方法执行完,result执行之前调用)接口
package com.test.listener; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.PreResultListener; public class MyListener implements PreResultListener { @Override public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("MyListener invoked"); } }
2. 在拦截器MyInterceptor中增加监听器
package com.test.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import com.test.listener.MyListener; public class MyInterceptor implements Interceptor { private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } @Override public void destroy() { System.out.println("destroy invoked"); } @Override public void init() { System.out.println("init invoked"); System.out.println("hello:"+hello); } @Override public String intercept(ActionInvocation invocation) throws Exception { invocation.addPreResultListener(new MyListener()); System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; } }
对于以上方式,只有一处会用到的一个类的一个简单方法,可以不用自定义监听器,直接在拦截器中用一个匿名类来实现,如下:
@Override public String intercept(ActionInvocation invocation) throws Exception { invocation.addPreResultListener(new PreResultListener(){ @Override public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("hello world"); } }); System.out.println("intercept invoked"); String result=invocation.invoke(); System.out.println("intercept invoke result:"+result); return result; }
九.拦截器应用的场合(最重要的应用是权限验证)
1. 定义权限拦截器AuthInterceptor.java
package com.test.interceptor; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthInterceptor extends AbstractInterceptor { @SuppressWarnings("unchecked") @Override public String intercept(ActionInvocation invocation) throws Exception { Map map=invocation.getInvocationContext().getSession(); if(null==map.get("user")){ //用户没有登录 return Action.LOGIN; }else{ return invocation.invoke(); //转到下一个拦截器 } } }
2. struts.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="struts2" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"> <param name="hello">world</param> </interceptor> <interceptor name="myInterceptor2" class="com.test.interceptor.MyInterceptor2"> </interceptor> <interceptor name="myInterceptor3" class="com.test.interceptor.MyInterceptor3"> </interceptor> //声明权限拦截器 <interceptor name="authInterceptor" class="com.test.interceptor.AuthInterceptor"> </interceptor> //使用权限拦截器 <interceptor-stack name="myStack"> <interceptor-ref name="authInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> //使用全局结果集,对所有Action生效 <global-results> //type的值,可以在struts-default.xml中查找,redirect是重定向,默认是dispatcher转发 <result name="login" type="redirect">/login.jsp</result> </global-results> <!-- <default-interceptor-ref name="myStack"></default-interceptor-ref> --> <action name="helloworld" class="com.test.action.HelloWorld"> <result>/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result>/result.jsp</result> <result name="input">/login2.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test" > <result name="success">/output.jsp</result> <result name="input">/error.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> <!-- <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="myInterceptor2"></interceptor-ref> --> <interceptor-ref name="myStack"></interceptor-ref> </action> </package> </struts>
发表评论
-
struts2.1改变配置文件默认位置
2011-03-31 14:01 752<filter> <init-par ... -
整合SSH2时需注意几点
2010-12-10 16:35 8201.需要导入struts2-spring-plugin-2.1 ... -
ssh2
2010-11-11 13:56 1831准备条件: 1. 建WEB工程:ssh2 2. 建数据库ssh ... -
spring整合hibernate
2010-11-08 08:59 802准备条件: 1. 创建数据库hibernatespring,表 ... -
代理模式
2010-11-08 08:58 816一. 静态代理 1. Subject.java packa ... -
单例模式
2010-11-08 08:57 6291. Singleton.java package com ... -
spring整合struts2
2010-11-08 08:56 717前言: 1. 创建工程strutsspring 2. 导入st ... -
反射案例
2010-11-08 08:56 685案例一: 1. Test.java package com ... -
spring配置文件中集合类型的配置
2010-11-08 08:54 926前言: Spring中对于有些bean会有集合类型的属性,以下 ... -
spring用到的设计模式-工厂模式
2010-11-08 08:53 1215Spring用到的设计模式之一 核心:用到工厂设计模式 1. ... -
读取applicationContext.xml的两种方式
2010-11-08 08:52 916第一种: ClassPathResource cpr=ne ... -
用图形查看spring配置文件依赖关系
2010-11-08 08:51 911myeclipse提供了这样的功能: window->s ... -
spring的基本操作IOC
2010-11-08 08:51 707前言: Spring通过IOC/DI来实现获取对象 准备: 1 ... -
spring的基本操作-反射
2010-11-08 08:50 1085前言: Spring通过反射来实现获取对象 准备: 1. 建S ... -
Struts2整合Hibernate
2010-10-29 19:00 19881. 准备条件: 1.1 在MYSQL中创建hibernate ... -
hibernate级联操作
2010-10-29 18:59 870准备条件: 1. 创建hibernate3工程 2. 导入hi ... -
hibernate基本操作
2010-10-29 18:58 7181.准备条件: 1.1 创建hibernate2工程 1.2 ... -
JDBC
2010-10-29 18:56 737一. JDBC连接Access实例 1. 准备条件 1.1 创 ... -
文件上传
2010-10-26 14:49 6621. upload.jsp <%@ page lan ... -
调用指定Action的方法
2010-10-26 14:48 735主要在配置文件中配置下: 注:如应用于增删改查的类 1. st ...
相关推荐
Struts2拦截器原理分析实例 博文链接:https://zmx.iteye.com/blog/457432
最全的InterCepter的基础,面向对象AOP的透彻分析
参考博文:http://blog.csdn.net/u011638419/article/details/41510483
参考博文:http://blog.csdn.net/u011638419/article/details/41592837
主要介绍了struts2过滤器和拦截器的区别,简单分析了struts2框架中过滤器和拦截器的概念与相关使用区别,需要的朋友可以参考下
IBMStruts2培训PPT (大全集) 主讲:刘雷 完整实用 第一章:认识体验Struts2.ppt 第二章:struts2架构剖析.ppt 第三章:struts2配置详解.ppt ...第九章:struts2拦截器.ppt + Struts2开发详解.ppt
主要为大家详细介绍了JavaWeb中Struts2拦截器的功能,感兴趣的小伙伴们可以参考一下
Struts拦截器Demo,适合理解和分析拦截器的用法。
action command在穿越了一系列Struts2和XWork的拦截器之后,最后执行用户编写的Action类,在Action执行之后,响应还会穿越相同的拦截器(按照与请求相反的顺序),最后通过Struts2的HttpServletResponse转换成Web可...
Struts2面试题分析_尚硅谷_佟刚 1. 简述 Struts2 的工作流程 2. Struts2 拦截器 和 过滤器 的区别 3. 为什么要使用 Struts2 & Struts2 的优点.....
第一章:认识体验Struts 2 第二章:struts2架构剖析 第三章:struts2配置详解 第四章:struts2中的OGNL 第五章:struts2标签库 第六章:struts2国际化 ...第九章:struts2拦截器 第十章:struts2类型转换
作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...
对struts2漏洞的分析,及解决方法,主要通过自定义一个拦截器来完成,简单有效
主要介绍了Struts2源码分析之ParametersInterceptor拦截器,ParametersInterceptor拦截器其主要功能是把ActionContext中的请求参数设置到ValueStack中,,需要的朋友可以参考下
主要对于对struts2高级框架感兴趣的,可以当参考文档使用,主要对拦截器详细分析
第10章 Model Driven和Preparable拦截器 第11章 持久层 第12章 文件的上传 第13章 文件的下载 第14章 提高Struts应用程序的安全性 第15章 防止重复提交 第16章 调试与性能分析 第17章 进度条 第18章 定制拦截器 第19...
主要介绍了Struts2中对未登录jsp页面进行拦截功能的实现,在演示源码的同时对步骤和原理进行了分析,具有一定参考价值,需要得朋友可以了解下。
每个技术点都配备案例和代码,对于拦截器、Struts2 运行流程分析、值栈等技术点的讲授更是深入解析源代码,授之以渔。 学习完本Java视频教程,你会真正理解 Struts2 的优雅和简洁,并使你具备开发 Struts2 企业...