首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

(转)Struts 二的基石——拦截器(Interceptor)

2013-08-13 
(转)Struts 2的基石——拦截器(Interceptor)?图1 拦截器调用序列图已有的拦截器Struts 2已经为您提供丰富多

(转)Struts 2的基石——拦截器(Interceptor)
?
图1 拦截器调用序列图

已有的拦截器

Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置。

(转)Struts 二的基石——拦截器(Interceptor)在本文使用是Struts 2的最新发布版本2.0.1。需要下载的朋友请点击以下链接:
http://apache.justdn.org/struts/binaries/struts-2.0.1-all.zip

以下部分就是从struts-default.xml文件摘取的内容:

<?interceptor?name?="alias"?class?="com.opensymphony.xwork2.interceptor.AliasInterceptor"?/>?
<?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,内容如下:

(转)Struts 二的基石——拦截器(Interceptor)?package?tutorial;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?com.opensymphony.xwork2.ActionSupport;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?public?class?TimerInterceptorAction?extends?ActionSupport?{
(转)Struts 二的基石——拦截器(Interceptor)?? ?@Override
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?String execute()?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??try?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ??//?模拟耗时的操作?
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ?Thread.sleep(?500?);
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?}?catch?(Exception e)?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ?e.printStackTrace();
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??return?SUCCESS;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)}

配置Action,名为Timer,配置文件如下:

<!?DOCTYPE struts PUBLIC
? ? ? ? "-//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页面后,查看服务器的后台输出。

2006?-?12?-?6?14?:?27?:?32?com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action?[?//Timer!execute?]?took?2859?ms.

在您的环境中执行Timer!execute的耗时,可能上述的时间有些不同,这取决于您PC的性能。但是无论如何,2859 ms与500 ms还是相差太远了。这是什么原因呢?其实原因是第一次加载Timer时,需要进行一定的初始工作。当你重新请求Timer.action时,以上输出会变为:

2006?-?12?-?6?14?:?29?:?18?com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action?[?//Timer!execute?]?took?500?ms.

OK,这正是我们期待的结果。上述例子演示了拦截器timer的用途——用于显示执行某个action方法的耗时,在我们做一个粗略的性能调试时,这相当有用。

自定义拦截器

作为“框架(framework)”,可扩展性是不可或缺的,因为世上没有放之四海而皆准的东西。虽然,Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。

?

(转)Struts 二的基石——拦截器(Interceptor)大家在开始着手创建自定义拦截器前,切记以下原则:
拦截器必须是无状态的,不要使用在API提供的ActionInvocation之外的任何东西。

要求拦截器是无状态的原因是Struts 2不能保证为每一个请求或者action创建一个实例,所以如果拦截器带有状态,会引发并发问题。

所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜欢继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor。

以下例子演示通过继承AbstractInterceptor,实现授权拦截器。

首先,创建授权拦截器类tutorial.AuthorizationInterceptor,代码如下:

(转)Struts 二的基石——拦截器(Interceptor)?package?tutorial;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?java.util.Map;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?com.opensymphony.xwork2.Action;
(转)Struts 二的基石——拦截器(Interceptor)?import?com.opensymphony.xwork2.ActionInvocation;
(转)Struts 二的基石——拦截器(Interceptor)?import?com.opensymphony.xwork2.interceptor.AbstractInterceptor;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?public?class?AuthorizationInterceptor?extends?AbstractInterceptor?{
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?? ?@Override
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?String intercept(ActionInvocation ai)?throws?Exception?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?Map session?=?ai.getInvocationContext().getSession();
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?String role?=?(String) session.get(?"?ROLE?"?);
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??if?(?null?!=?role)?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ?Object o?=?ai.getAction();
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ??if?(o?instanceof?RoleAware)?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ? ? ?RoleAware action?=?(RoleAware) o;
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ? ? ?action.setRole(role);
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ??return?ai.invoke();
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?}?else?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ? ? ??return?Action.LOGIN;
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?}?? ? ? ?
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?
(转)Struts 二的基石——拦截器(Interceptor)}

以上代码相当简单,我们通过检查session是否存在键为“ROLE”的字符串,判断用户是否登陆。如果用户已经登陆,将角色放到Action中,调用Action;否则,拦截直接返回Action.LOGIN字段。为了方便将角色放入Action,我定义了接口tutorial.RoleAware,代码如下:

(转)Struts 二的基石——拦截器(Interceptor)?package?tutorial;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?public?interface?RoleAware?{
(转)Struts 二的基石——拦截器(Interceptor)?? ??void?setRole(String role);
(转)Struts 二的基石——拦截器(Interceptor)}

接着,创建Action类tutorial.AuthorizatedAccess模拟访问受限资源,它作用就是通过实现RoleAware获取角色,并将其显示到ShowUser.jsp中,代码如下:

(转)Struts 二的基石——拦截器(Interceptor)?package?tutorial;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?com.opensymphony.xwork2.ActionSupport;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?public?class?AuthorizatedAccess?extends?ActionSupport?implements?RoleAware?{
(转)Struts 二的基石——拦截器(Interceptor)?? ??private?String role;
(转)Struts 二的基石——拦截器(Interceptor)?? ?
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?void?setRole(String role)?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??this?.role?=?role;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?? ?
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?String getRole()?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??return?role;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?
(转)Struts 二的基石——拦截器(Interceptor)?? ?@Override
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?String execute()?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??return?SUCCESS;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)}

以下是ShowUser.jsp的代码:

<%?@ page ?contentType?=?"?text/html; charset=UTF-8?"?%>?
<%?@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初始化角色列表,代码如下:

(转)Struts 二的基石——拦截器(Interceptor)?package?tutorial;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?java.util.Hashtable;
(转)Struts 二的基石——拦截器(Interceptor)?import?java.util.Map;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?public?class?Roles?{
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?Map?<?String, String?>?getRoles()?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?Map?<?String, String?>?roles?=?new?Hashtable?<?String, String?>?(?2?);
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?roles.put(?"?EMPLOYEE?"?,?"?Employee?"?);
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?roles.put(?"?MANAGER?"?,?"?Manager?"?);
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??return?roles;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)}

接下来,新建Login.jsp实例化tutorial.Roles,并将其roles属性赋予<s:radio>标志,代码如下:

<%?@ page ?contentType?=?"?text/html; charset=UTF-8?"?%>?
<%?@taglib prefix?=?"?s?"?uri?=?"?/struts-tags?"?%>?
<?html?>?
<?head?>?
? ??<?title?>?Login?</?title?>?
</?head?>?
<?body?>?
? ??<?h1?>?Login?</?h1?>?
? ? Please select a role below:
? ??<?s:bean?id?="roles"?name?="tutorial.Roles"?/>?
? ??<?s:form?action?="Login"?>?
? ? ? ??<?s:radio?list?="#roles.roles"?value?="'EMPLOYEE'"?name?="role"?label?="Role"?/>?
? ? ? ??<?s:submit?/>?
? ??</?s:form?>?
</?body?>?
</?html?>

创建Action类tutorial.Login将role放到session中,并转到Action类tutorial.AuthorizatedAccess,代码如下:

(转)Struts 二的基石——拦截器(Interceptor)?package?tutorial;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?java.util.Map;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?org.apache.struts2.interceptor.SessionAware;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?import?com.opensymphony.xwork2.ActionSupport;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?public?class?Login?extends?ActionSupport?implements?SessionAware?{
(转)Struts 二的基石——拦截器(Interceptor)?? ??private?String role; ? ?
(转)Struts 二的基石——拦截器(Interceptor)?? ??private?Map session;
(转)Struts 二的基石——拦截器(Interceptor)
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?String getRole()?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??return?role;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?void?setRole(String role)?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??this?.role?=?role;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?? ?
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?void?setSession(Map session)?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??this?.session?=?session;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?
(转)Struts 二的基石——拦截器(Interceptor)?
(转)Struts 二的基石——拦截器(Interceptor)?? ?@Override
(转)Struts 二的基石——拦截器(Interceptor)?? ??public?String execute()?{
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ?session.put(?"?ROLE?"?, role);
(转)Struts 二的基石——拦截器(Interceptor)?? ? ? ??return?SUCCESS;
(转)Struts 二的基石——拦截器(Interceptor)?? ?}?? ?
(转)Struts 二的基石——拦截器(Interceptor)}

最后,配置struts.xml文件,内容如下:

<!?DOCTYPE struts PUBLIC
? ? ? ? "-//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"?>?
? ? ? ??<?interceptors?>?
? ? ? ? ? ??<?interceptor?name?="auth"?class?="tutorial.AuthorizationInterceptor"?/>?
? ? ? ??</?interceptors?>?
? ? ? ??<?action?name?="Timer"?class?="tutorial.TimerInterceptorAction"?>?
? ? ? ? ? ??<?interceptor-ref?name?="timer"?/>?
? ? ? ? ? ??<?result?>?/Timer.jsp?</?result?>?
? ? ? ??</?action?>?
? ? ? ??<?action?name?="Login"?class?="tutorial.Login"?>?
? ? ? ? ? ??<?result?type?="chain"?>?AuthorizatedAccess?</?result?>?
? ? ? ??</?action?>?
? ? ? ??<?action?name?="AuthorizatedAccess"?class?="tutorial.AuthorizatedAccess"?>?
? ? ? ? ? ??<?interceptor-ref?name?="auth"?/>?
? ? ? ? ? ??<?result?name?="login"?>?/Login.jsp?</?result?>?
? ? ? ? ? ??<?result?name?="success"?>?/ShowRole.jsp?</?result?>?
? ? ? ??</?action?>?
? ??</?package?>?
</?struts?>

发布运行应用程序,在浏览器地址栏中输入:http://localhost:8080/Struts2_Interceptor/AuthorizatedAccess.action。由于此时,session还没有键为“ROLE”的值,所以返回Login.jsp页面,如图2所示:

(转)Struts 二的基石——拦截器(Interceptor)?
图2 Login.jsp

选中Employee,点击Submit,出现图3所示页面:

(转)Struts 二的基石——拦截器(Interceptor)?
图3 ShowRole.jsp

总结

拦截器是Struts 2比较重要的一个功能。通过正确地使用拦截器,我们可以编写高可复用的代码。

热点排行