您的位置:首页 > 编程语言 > Java开发

关于Struts2的架构 学习笔记【译】

2010-03-20 22:45 375 查看
今晚看了一晚上的Struts2的源代码,对于整个过程有了更深入的了解,并尝试把学习的资料翻译一下,锻炼一下自己的英语翻译能力。

对于一些自己的想法也会进行旁注。初次翻译,感觉还是比较难的,有翻译错误地方,还望指正。

对于觉得比较有兴趣的地方有:拦截器的调用过程(DefaultActionInvocation.invoke())、还有性能的监控(UtilTimerStack类)。

时间问题等待以后再作整理

关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类

==========================

Struts2 是一个基于多种标准技术组合的可自由扩展调控的技术,包含相关标准有Java Filters, JavaBeans, ResourceBundles, Locales, and XML,同样也包含多种OpenSymphony
开源库:OGNL和XWork  (Struts2 核心是XWork2)

Struts is a flexible control layer based on standard technologies
like Java Filters, JavaBeans, ResourceBundles, Locales, and XML, as
well as various OpenSymphony



packages, like OGNL and XWork.

 

Struts2框架借助其模型(实体)可以很好地兼容多种标准数据操作技术,例如JDBC


、EJB


以及其他的第三方库Cayenne



, Hibernate



, or iBATIS



.

For the Model, the framework can interact with standard data access technologies, like JDBC



and EJB



, as well as most any third-party packages, like Cayenne



, Hibernate



, or iBATIS



.

 

同样借助标准的视图模型、框架也可以各种标准的页面展示技术进行交互,主要的页面技术包括:JSTL、JSF、FreeMarker



Velocity



Templates, PDF, XSLT。

For the View, the framework works well with JavaServer Pages



, including JSTL and JSF, as well as FreeMarker



or Velocity



Templates, PDF, XSLT, and other presentation systems.

框架中,我们不仅可以自由地设置Action和Result,还可以自定义相关的异常处理机制和拦截器。

Aside from actions and results, we can specify exception handlers
and interceptors
.

异常处理机制 可以很好地处理一些全局或局部的异常,利用它我们不再需要在代码里面加入大量的try..catch的语句,并且可以指定捕获对应的异常及显示错误的页面,页面可以显示适当的说明及原因,这样能显得更友好一些。

     
                                                                                                                                                                Exception handlers
declare how to handle an
exception on a global or local basis. Instead of sprinking source code
with try .. catch blocks, we can let the framework catch the exception
and display the page of our choice. The page can display an appropriate
message and details from the exception.

拦截器 将存在于一个Action里面的“请求 -》处理 ”的过程。(可以在Action处理前或处理后触发执行。)拦截器可以指定为全局或者局部的。当有比较快速且简单业务逻辑的请求时,可以进一步去简化拦截器机制,例如:Ajax、SOAP、JSF请求。如果有特别需求还可以设置“直通”(无相关执行代码、或者很少量的代码)的拦截器,当然这种情况是比较少出现的。 在一般情况下,我们只要指定默认的拦截器集就已经足够了,拦截器集是由多种拦截器组成。只要指定拦截器集,就可以同时使用多个拦截器。

       Interceptors
specify the "request-processing
lifecycle" for an action. (What happens to the request before and after
the Action class fires.) We can specify both global and local
lifecycles. If some actions respond to AJAX, SOAP, or JSF requests, we
can simplify the lifecycle, and even just "pass through" the request,
if needed. However, these are special cases, and most applications can
use the default interceptor stack, "out of the box".

 

以下的图表展示了Struts2的主要框架和相关类。

The diagram describes the framework's architecture.



 

在图表中,可以看到用户的一个请求通过像Jetty、Resin的标准Servlet容器被初始化后,还需要再通过一些相关的过滤器,这些过滤器包括ActionContextCleanUp
过滤器(在集合其他技术时很有用,可以不加上)、FilterDispatcher
过滤器(关键过滤器,必须加上,在处理请求时会在根据ActionMapper要决定哪一个Action将被调用。)

In the diagram, an initial request goes to the Servlet container
(such as Jetty or Resin) which is passed through a standard filter
chain. The chain includes the (optional) ActionContextCleanUp
filter, which is useful when integrating technologies such as SiteMesh Plugin
. Next, the required FilterDispatcher
is called, which in turn consults the ActionMapper
to determine if the request should invoke an action.

 

根据ActionMapper指定了哪一个Action将被执行、FilterDispatcher就会将控制权委托给ActionProxy类。 由ActionProxy去根据框架的相关配置文件管理器(由Struts.xml文件初始化)创建对应的ActionInvocation(Action的代理,基于Command模式实现)。利用调用Action本身从而执行每一个配置在Action上的拦截器(前置)。

If the ActionMapper determines that an Action should be invoked, the FilterDispatcher delegates control to the ActionProxy
. The ActionProxy consults the framework Configuration Files
manager (initialized from the struts.xml
file). Next, the ActionProxy creates an ActionInvocation
, which is responsible for the command pattern implementation. This includes invoking any Interceptors
(the before
clause) in advance of invoking the Action
itself.

 

当Action执行后,ActionInvocation类会根据Action返回的Results类在配置文件struts.xml(或者其他子配置文件)中查找对应的响应result。再由相关的页面展示技术,像JSP、Freemarker来组织页面内容,在组织响应内容时,可以使用框架提供的Struts标签。此外,一些特别的标签必须结合ActionMapper来组织页面内容。

Once the Action returns, the ActionInvocation is responsible for looking up the proper result
associated with the Action result code
mapped in struts.xml
. The result is then executed, which often (but not always, as is the case for Action Chaining
) involves a template written in JSP
or FreeMarker
to be rendered. While rendering, the templates can use the Struts Tags
provided by the framework. Some of those components will work with the
ActionMapper to render proper URLs for additional requests.


所有在框架里面的对象都由ObjectFactory来进行初始化,像Action、Result、Interceptor这些。同时ObjectFactory是插件式地加入到框架中,所以我们可以创建自己的ObjectFactory来控制对象的创建过程。比较经常用的是Spring的StrutsSpringObjectFactory。

All objects in this architecture (Actions, Results
, Interceptors
, and so forth) are created by an ObjectFactory
.
This ObjectFactory is pluggable. We can provide our own ObjectFactory
for any reason that requires knowing when objects in the framework are
created. A popular ObjectFactory implementation uses Spring as provided
by the Spring Plugin
.

拦截器将会被倒序地执行一次(后置执行语句部分),最后,响应的内容再回到最原始的过滤器(在web.xml中配置的)。如果ActionContextCleanUp有配置时,FilterDispatcher将不会清理ThreadLocal的线程内部变量ActionContext;如果没有设置ActionContextCleanUp,FilterDispatcher将会清理所有的ThreadLocal线程内部变量。但最终还是由垃圾回收机制回收,只是加快了回收速度。

Interceptors are executed again (in reverse order, calling the after
clause). Finally, the response returns through the filters configured in the web.xml
. If the ActionContextCleanUp filter is present, the FilterDispatcher will not
clean up the ThreadLocal ActionContext
. If the ActionContextCleanUp filter is not present, the FilterDispatcher will cleanup all ThreadLocals.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息