Struts2拦截器的配置
struts2拦截器interceptor的三种配置方法
方法1. 普通配置法
<struts>
<package name="struts2" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="edu.zd.core.MyInterceptor"></interceptor>
</interceptors> <action name="register" class="edu.zd.action.RegisterAction">
<result name="input">/register.jsp</result>
<result>/result.jsp</result> <!-- 在自定义interceptor并将其ref时, 系统会覆盖掉默认的interceptor-stack(defaultStack), 为了保证系统默认的defaultStack不受印象, 我们需要显式的将其引入 -->
<!-- 注意两个interceptor-ref的顺序, 顺序不同, 执行效果也不同: 先配置的先执行/后配置的先退出(先进后出) -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor"></interceptor-ref>
</action>
</package>
</struts>
方法2. 配置拦截器栈(即将多个interceptor串联的一种元素)。然后在<action>中引入该拦截器栈就可以了。
<struts>
<package name="struts2" extends="struts-default"> <interceptors>
<interceptor name="myInterceptor" class="edu.zd.interceptor.MyInterceptor"></interceptor> <interceptor-stack name="myInterceptorStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors> <action name="register" class="edu.zd.action.RegisterAction">
<result name="input">/register.jsp</result>
<result>/result.jsp</result> <interceptor-ref name="myInterceptorStack"></interceptor-ref>
</action>
</package>
</struts>
方法3. 修改默认拦截器,将自定义的拦截器栈定义为struts2的默认拦截器。
<struts>
<package name="struts2" extends="struts-default"> <interceptors>
<interceptor name="myInterceptor" class="edu.zd.interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors> <!-- 此默认interceptor是针对所有action的 -->
<!-- 如果某个action中引入了interceptor, 则在这个action中此默认interceptor就会失效 -->
<default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref> <action name="register" class="edu.zd.action.RegisterAction">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
</action> </package>
</struts>
2. Interceptor的角色对象
(1)拦截目标对象(被代理对象),这里目标对象就是action;
(2)拦截器(一个类,动态的将某些方法插入到目标对象的某方法的before、after);
(3)对目标对象生成的(动态)代理对象(代理对象内部方法综合了目标对象方法+拦截器方法)。程序最终执行的是目标对象的代理,而这个代理已经插入了interceptor。
拦截器作用:拦截action。interceptor相当于一个入口和出口,通过interceptor进入action,执行完action的代码再通过interceptor出去。
针对"struts2 -- interceptor(Interceptor怎么写)"这篇文章的MyInterceptor.class和MyInterceptor2.class。根据下面的配置文件执行程序
<struts>
<package name="struts2" extends="struts-default"> <interceptors>
<interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
<interceptor name="myInterceptor2" class="edu.hust.interceptor.MyInterceptor2"></interceptor>
<interceptor-stack name="myInterceptorStack">
<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="myInterceptorStack"></default-interceptor-ref> <action name="register" class="edu.hust.action.RegisterAction">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
</action> </package>
</struts>
Console会得到以下打印输出
intercept start
intercept2 start
2008-9-19 19:42:06 com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>
信息: Detected AnnotationActionValidatorManager, initializing it...
intercept2 finish
intercept finish
这个结果解释了"interceptor相当于一个入口和出口,通过interceptor进入action,执行完action的代码再通过interceptor出去"这句话。
3. extends MethodFilterInterceptor的拦截器如何配置哪些方法该拦截、哪些方法不该拦截(针对方法拦截的配置)
<struts>
<package name="struts2" extends="struts-default"> <interceptors>
<interceptor name="myInterceptor3" class="edu.hust.interceptor.MyInterceptor3"></interceptor>
</interceptors> <action name="register" class="edu.hust.action.RegisterAction" method="queryAll">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
<!-- myInterceptor3拦截器只对RegisterAction中的queryAll()方法和insert()方法进行了拦截, 其他方法未进行拦截 -->
<interceptor-ref name="myInterceptor3">
<param name="includeMethods">queryAll, execute</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action> <action name="register" class="edu.hust.action.RegisterAction" method="insert">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
<interceptor-ref name="myInterceptor3">
<param name="includeMethods">queryAll, insert</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action> <action name="register" class="edu.hust.action.RegisterAction" method="update">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
<interceptor-ref name="myInterceptor3">
<param name="includeMethods">queryAll, insert</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action> </package>
</struts>
Struts2拦截器的配置的更多相关文章
- 转载 - Struts2 拦截器详细配置过程
出处:http://www.blogjava.net/zzzlyr/archive/2009/10/12/297998.html Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Inter ...
- Struts2拦截器详解
一.Struts2拦截器原理: Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列 ...
- Struts2 拦截器配置以及实现
@(Java ThirdParty)[Struts|Interceptor] Struts2 拦截器配置以及实现 Struts2的拦截器应用于Action,可以在执行Action的方法之前,之后或者两 ...
- struts2拦截器interceptor的三种配置方法
1.struts2拦截器interceptor的三种配置方法 方法1. 普通配置法 <struts> <package name="struts2" extend ...
- Struts2拦截器配置
1. 理解拦截器 1.1. 什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AO ...
- Struts2拦截器配置实例
拦截器介绍 拦截器 的使用 ,源自Spring AOP(面向切面编程)思想 拦截器 采用 责任链 模式 * 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链. * 责任链每一个节 ...
- struts2拦截器interceptor的配置方法及使用
转: struts2拦截器interceptor的配置方法及使用 (2015-11-09 10:22:28) 转载▼ 标签: it 365 分类: Struts2 NormalText Code ...
- Struts2拦截器配置和使用
拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制. 说到拦截器interceptor,就会想到过滤器filter: 过滤器f ...
- Struts2 拦截器配置及使用
在我的项目中有个需求,实现记录用户操作的系统日志,基于这个功能我首先想到的是Struts 的拦截器.配置一个全部Action都会拦截的拦截,写一个公用的服务.每当用户发送请求到Action 就记录相应 ...
随机推荐
- ZedGraph 柱状图、饼图、折线图演示源码
http://code1.okbase.net/codefile/ZedGraphControl.ContextMenu.cs_201211225626_97.htm // //This librar ...
- json包含单双引号问题解决方案
解决方案:在后台处理 JSONArray.fromObject(list).toString() 转自明明如月小角落: 效果DEMO: JsonQuotesUtil.js /** * 解决json传输 ...
- TroubleShoot: Fail to deploy Windows UAP to device: 0x80073CFD
After creating "Blank App(Windows Universal)" targeting Windows Phone 10 in Visual Studio ...
- matplot 代码实例
matplot 代码实例 #!/usr/bin/env python # coding=utf-8 import numpy as np import matplotlib.pyplot as plt ...
- [UE4]ue4 c++学习推荐
我由易到难推荐,不过在此之前还是先看看官方对于VS设置的推荐: https://docs.unrealengine.com/latest/INT/Programming/Development/Vis ...
- 组件Slate教程 & UMG widget构造初始化函数中获取其内部组件
转自:http://aigo.iteye.com/blog/2296218 目的:在自定义的Widget初始化完毕后,获取其内部的button.combo等UMG组件的C++指针. 这里我们新建了一个 ...
- Spark Standalone模式HA环境搭建
Spark Standalone模式常见的HA部署方式有两种:基于文件系统的HA和基于ZK的HA 本篇只介绍基于ZK的HA环境搭建: $SPARK_HOME/conf/spark-env.sh 添加S ...
- java字符串分解 StringTokenizer用法
Java中substring方法可以分解字符串,返回的是原字符串的一个子字符串.如果要讲一个字符串分解为一个一个的单词或者标记,StringTokenizer可以帮你. 先看个例子: 1 public ...
- php 学习笔记 设计和管理
代码管理 文件路径.数据库名.密码禁止 hard coded 避免重复代码在多个页面复制粘贴 Gang of Four eXtreme Programming 的主要原则是坚决主张测试是项目成功的关键 ...
- noip 2011 选择客栈
题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...