- 浏览: 340664 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (97)
- 搜索引擎 (9)
- JAVA基础知识 (7)
- Struts (13)
- Spring (1)
- Hibernate (0)
- iBATIS (2)
- ExtJs (10)
- AJAX (0)
- Oracle (1)
- MSSQLSERVER (1)
- MySQL (0)
- Prototype (0)
- DWR (0)
- EJB (0)
- J2EE综合 (0)
- 算法 (1)
- WebServices (0)
- JMS (0)
- JavaMail (0)
- JMX (0)
- UML (0)
- Dojo (0)
- Acegi (2)
- 其他Html-XML等 (3)
- CSS (1)
- 其他开源项目 (0)
- 英语 (0)
- Web3D (24)
- FCKeditor (1)
- javascript 特效 (2)
最新评论
-
荷戟者:
如何实现的,是否可以想象一点
extjs实现左侧tab -
coconut_zhang:
怎么没人评论,吼吼。。
HTMLParser使用详解(4)- 通过Visitor访问内容 -
Sorry':
JDBC中处理存储过程的结果集的通用流程 -
Sorry':
计划将客户
JDBC中处理存储过程的结果集的通用流程 -
renhongchao:
very good!在csdn也看到相同的文章,莫非是同一人? ...
HTMLParser使用详解(3)- 通过Filter访问内容
什么是拦截器
拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
在 Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。
实现原理
Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器,如图1所示。
图1 拦截器调用序列图
已有的拦截器
Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置。
在本文使用是Struts 2的最新发布版本2.0.1。需要下载的朋友请点击以下链接: http://apache.justdn.org/struts/binaries/struts-2.0.1-all.zip |
以下部分就是从struts-default.xml文件摘取的内容:
< interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />
< interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />
< interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />
< interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />
< interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
< interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />
< interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />
< interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />
< interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" />
< interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />
< interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />
< interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
< interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />
< interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
< interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />
< interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
< interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />
< interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />
< interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />
< interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />
< interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />
< interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
< interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />
< interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />
< interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" />
< interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" />
< interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
配置和使用拦截器
在struts-default.xml中已经配置了以上的拦截器。如果您想要使用上述拦截器,只需要在应用程序struts.xml文件中通过“<include file="struts-default.xml" />”将struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用“<interceptor-ref name="xx" />”引用拦截器或拦截器栈(interceptor stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。当然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆盖defaultStack。
下面是关于拦截器timer使用的例子。首先,新建Action类tuotrial/TimerInterceptorAction.java,内容如下:
import com.opensymphony.xwork2.ActionSupport;
public class TimerInterceptorAction extends ActionSupport {
@Override
public String execute() {
try {
// 模拟耗时的操作
Thread.sleep( 500 );
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
}
配置Action,名为Timer,配置文件如下:
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< include file ="struts-default.xml" />
< package name ="InterceptorDemo" extends ="struts-default" >
< action name ="Timer" class ="tutorial.TimerInterceptorAction" >
< interceptor-ref name ="timer" />
< result > /Timer.jsp </ result >
</ action >
</ package >
</ struts >
至于Timer.jsp可以随意写些什么到里面。发布运行应用程序,在浏览器的地址栏键入http://localhost:8080/Struts2_Interceptor/Timer.action,在出现Timer.jsp页面后,查看服务器的后台输出。
信息: Executed action [ //Timer!execute ] took 2859 ms.
在您的环境中执行Timer!execute的耗时,可能上述的时间有些不同,这取决于您PC的性能。但是无论如何,2859 ms与500 ms还是相差太远了。这是什么原因呢?其实原因是第一次加载Timer时,需要进行一定的初始工作。当你重新请求Timer.action时,以上输出会变为:
信息: Executed action [ //Timer!execute ] took 500 ms.
OK,这正是我们期待的结果。上述例子演示了拦截器timer的用途——用于显示执行某个action方法的耗时,在我们做一个粗略的性能调试时,这相当有用。
自定义拦截器
作为“框架(framework)”,可扩展性是不可或缺的,因为世上没有放之四海而皆准的东西。虽然,Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。
大家在开始着手创建自定义拦截器前,切记以下原则: 拦截器必须是无状态的,不要使用在API提供的ActionInvocation之外的任何东西。 |
要求拦截器是无状态的原因是Struts 2不能保证为每一个请求或者action创建一个实例,所以如果拦截器带有状态,会引发并发问题。
所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜欢继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor。
以下例子演示通过继承AbstractInterceptor,实现授权拦截器。
首先,创建授权拦截器类tutorial.AuthorizationInterceptor,代码如下:
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorizationInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation ai) throws Exception {
Map session = ai.getInvocationContext().getSession();
String role = (String) session.get( " ROLE " );
if ( null != role) {
Object o = ai.getAction();
if (o instanceof RoleAware) {
RoleAware action = (RoleAware) o;
action.setRole(role);
}
return ai.invoke();
} else {
return Action.LOGIN;
}
}
}
以上代码相当简单,我们通过检查session是否存在键为“ROLE”的字符串,判断用户是否登陆。如果用户已经登陆,将角色放到Action中,调用Action;否则,拦截直接返回Action.LOGIN字段。为了方便将角色放入Action,我定义了接口tutorial.RoleAware,代码如下:
public interface RoleAware {
void setRole(String role);
}
接着,创建Action类tutorial.AuthorizatedAccess模拟访问受限资源,它作用就是通过实现RoleAware获取角色,并将其显示到ShowUser.jsp中,代码如下:
import com.opensymphony.xwork2.ActionSupport;
public class AuthorizatedAccess extends ActionSupport implements RoleAware {
private String role;
public void setRole(String role) {
this .role = role;
}
public String getRole() {
return role;
}
@Override
public String execute() {
return SUCCESS;
}
}
以下是ShowUser.jsp的代码:
<% @taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > Authorizated User </ title >
</ head >
< body >
< h1 > Your role is: < s:property value ="role" /></ h1 >
</ body >
</ html >
然后,创建tutorial.Roles初始化角色列表,代码如下:
import java.util.Hashtable;
import java.util.Map;
public class Roles {
public Map < String, String > getRoles() {
Map < String, String > roles = new Hashtable < String, String > ( 2 );
roles.put( " EMPLOYEE " , " Employee " );
roles.put( " MANAGER " , " Manager " );
return roles;
}
}
接下来,新建Login.jsp实例化tutorial.Roles,并将其roles属性赋予<s:radio>标志,代码如下:
发表评论
-
详解struts2中struts.properties
2007-10-25 00:17 2682Stru ... -
struts2的struts.properties配置文件详解
2007-10-24 23:55 2165struts.action.extension ... -
Struts 2 间接实现零配置
2007-10-19 11:41 1741所谓间接实现零配置,是指只要做些初始化的配置之后,在以后的开发 ... -
Parameters in configuration results
2007-10-10 17:12 1183Parameters in configura ... -
How do we upload files
2007-10-10 16:47 1447xml 代码 How do we upload ... -
display dynamic or static images
2007-10-10 16:38 1605How can we display dynamic or s ... -
Struts2 验证器
2007-10-08 22:30 4341<validators> ... -
初涉Struts 2.0
2007-10-06 09:43 1165初涉Struts 2.0... -
Apache Struts 2 Plugin Registry
2007-10-03 17:25 2110Apache Struts 2 Plugin Registry ... -
Struts 2 官方
2007-10-03 17:22 11375Struts 2 官方: http://struts.apac ... -
struts2与spring2集成
2007-10-03 17:12 1200在Eclipse下建立一个Dynamic Web Applic ... -
struts2的struts.properties配置文件详解
2007-10-03 17:02 1136struts.action.extension ... -
Struts 2与AJAX(第三部分)
2007-10-03 16:53 1482Struts 2与AJAX(第三部分 ... -
Struts 2与AJAX(第二部分)
2007-10-03 16:52 1622在上一篇文章《Struts 2与AJAX(第一部分)》,我已经 ... -
Struts 2与AJAX(第一部分)
2007-10-03 16:51 1642Struts 2与AJAX(第一部分) 在当今——Web 2 ... -
struts2表单标签
2007-10-03 16:48 1597Form Tags ... -
Struts2 with logon
2007-10-03 16:45 1137关于此工程中的所有设置是承接前一篇文章Struts2介绍之使用 ... -
Struts2介绍之使用链接标签
2007-10-03 16:42 1841Web应用区别于普通的站点之处在于Web应用可以创建一个动态的 ... -
Strus 2的新表单标志的使用
2007-10-03 16:35 1125Struts 2为大家提供了不少常用的很酷的表单标志,简化了我 ... -
在Struts 2中实现文件上传
2007-10-03 16:33 1199实现原理 Struts 2是通过Commons FileUpl ...
相关推荐
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
NULL 博文链接:https://wang4717.iteye.com/blog/796941
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
STRUTS2:拦截器Interceptor
struts2 Interceptor拦截器 http://whitewolf.0fees.net
简单的拦截器例子,适合初学者学习,也是一个备忘录
07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 Struts 2的新表单标志的使用 13 Struts 2与AJAX
07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX
Struts 2.0系列(MAX),pdf格式,全...Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD Struts 2中的OGNL Strus 2的新表单标志的使用 Struts 2与AJAX
利用反射和动态代理机制实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34087833
struts2 拦截器_教程.zip
谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链
07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX Struts2中用...
在struts2中使用拦截器(Interceptor)控制登录和权限[汇编].pdf
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
Struts2_自定义拦截器 struts2_3500_my_interceptor
1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. 拦截器栈(Interceptor Stack)。Struts2拦截器栈就是将拦截器按...
创建自己struts2拦截器,手工创建自己的拦截器实现
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.