<!--StartFragment-->小弟最近正在学习Struts2,由SpringSide的文档里面发现infoq上有这个系列的文章,觉得不错,但是无奈英文太差。所以就把我对这个文章的理解写了出来。红色标出来的地方都是我没什么把握的,或者压根就看不明白的,希望各位老大能够指点一下,多谢多谢了!
Migrating Struts Apps to Struts 2
The first version of Struts was released in June 2001. It was born out of the idea that JSPs and servlets could be used together to provide a clean separation between the view and the business or application logic of a web application. Before Struts, the most common options were to add business and application logic to the JSP, or to render the view from servlets using println() statements.
Since its release, Struts has become the de-facto standard for web application. With this popularity have come enhancements and changes - both to keep up with the ever-changing requirements for a web application framework, but also to match features with the ever-increasing number of competing frameworks available.
To that end, there have been several proposals for the next generation of Struts. The two alternatives that have become the most cohesive in the past year are Shale and Struts Ti. Shale is a component based framework, and just recently has become its own top-level Apache project, where Struts Ti continues the front controller or model-view-controller pattern that has made Struts so successful.
The WebWork project was started as a Struts revolution - as a fork of the Struts code to introduce new ideas, concepts and functionality that may not be compatible with the original Struts code - and it was first released in March 2002. WebWork is a mature framework, having undergone several minor and major releases.
In December 2005 it was announced that WebWork and the Struts Ti would join forces. Since that time, Struts Ti has become Struts Action Framework 2.0, and the successor to Struts.
Finally, it should be noted that neither the Struts nor WebWork projects are going away. While interest is high, and willing developers are available, both of these projects will continue - all the while having bugs fixed, and adding enhancements and new features.
这一段就不翻译了,都是些陈芝麻烂谷子,没什么太大用。
Before we start looking at the low level details of how to convert an application from Struts to Struts2, let's take a look at what the new architecture looks like by walking through the request processing.
As we walk through the request lifecycle you should notice one important fact - Struts2 is still a front controller framework. All of the concepts that you are familiar with will still apply.
在我们开始关注将一个Struts应用转换为struts2应用的细节问题之间,让我们先走一遍请求处理的流程,来了解一下struts2的新架构
在我们走一遍请求的生命周期之后,你会注意到一个重要的事实--struts2仍然是一个front controller framework(前端控制框架?)。你所熟悉的相关概念仍然适用(这里应该指的是sturts当中的一些关于请求处理的概念)
This means:
From a high-level overview, this is how the request is processed:
在一个相对比较高的级别上来看,请求的处理过程如下图:
The processing of a request can be broken into these 6 steps:
As you may have noticed, there are some differences. The most obvious one is that Struts2 is a pull-MVC architecture. What does this mean? From a developers perspective it means that data that needs to be displayed to the user can be pulled from the Action. This differs from Struts, where data is expected to be present in beans in either the HTTP page, request or session scopes. There are other places that data can be pulled from, and we'll talk about those as the scenarios come up.
你可能已经注意到了,这里有一些差别。一个明显的差别是Struts2是一个pull(推)MVC架构。这是什么意思呢。从一个开发者的角度来看,这意味着是由Action pull(推)出了需要显示给客户的数据。而在Struts当中,数据被封装在bean当中,然后被放在page,request或session当中。Action还可以将数据pull到别的地方,and we'll talk about those as the scenarios come up.
The first, and most important configuration, is the one that enables the web application framework within the servlet containers web.xml file.
The configuration that everyone should be familiar with for Struts is:
首先最重要的配置是让web应用程序框架能够在Servlet容器当中运行,需要通过修改web.xml来实现.
对Struts的配置大体上如下:
For Struts2 there are very few changes. The most significant is that the dispatcher has been changed from a servlet to a servlet filter. The configuration is just as easy as for a servlet, and shown here:
Similar to the servlet configuration, the filter configuration defines a name (for reference) and the class of the filter. A filter mapping links the name with the URI pattern that will invoke the filter. By default, the extension is ".action". This is defined in the default.properties file (within the Struts2 JAR file) as the "struts.action.extension" property.
SIDEBAR: The "default.properties" file is the place that many configuration options are defined. By including a file with the name "struts.properties" in the classpath of your web application, with different values for the properties, you can override the default configuration.
For Struts, the servlet configuration provides an init-param tag that defines the names of the files used to configure Struts. Struts2 does not have such a configuration parameter. Instead, the default configuration file for Struts2 has the name "struts.xml" and needs to be on the classpath of the web application.
SIDEBAR/TIP: Since there is a namespace separation between the Struts actions (with a ".do" extension) and the Struts2 actions (with a ".action") extension, there is no reason why they cannot co-exist within the same web application. This is a great way to start the migration process - add the necessary configuration, and start developing all new functionality in Struts2. As time and resources permit, the remaining actions can be converted. Or, the two frameworks can co-exist peacefully forever, since there is no reason that the existing action ever needs to be migrated. Another migration strategy is to update only the actions by changing the Struts2 extension to ".do". This allows the existing JSP's to remain the same and be re-used.
In the request walk-through we spoke about some of the differences between Struts and Struts2 from a high level. Let's take it a step deeper now, and look at the differences between the structures of the actions in each framework.
Let's first review the general structure of the Struts action. The general form of the Struts action looks like this:
在request walk-through当中,我们讲了一些Struts和Struts在较高层面上的区别,现在让我们深入一些,来看看在这两个框在在Action上面的区别。
让我们首先回顾一下Struts的action的结构。一个Struts的Action应该是如下的这个样子:
When implementing a Struts action, you need to be aware of the following items:
In contrast, the Struts2 action provides a much simpler implementation. Here's what it looks like:
相比之下,Struts2的Action提供了一个更加简单的实现:
The first thing you may have noticed is that the action doesn't extend any classes or interfaces. In fact, it goes further than this. By convention, the method invoked in the processing of an action is the "execute" method - but it doesn't have to be. Any method that follows the method signature public String methodName() can be invoked through configuration.
Next, the return object is a String. If you don't like the idea of string literals in your code, there is a helper interface Action available that provides the common results of "success", "none", "error", "input" and "login" as constants.
Finally, and perhaps the most revolutionary difference from the original Struts implementation, is that the method invoked in the processing of an action (the "execute" method) has no parameter. So how do you get access to the objects that you need to work with? The answer lies in the "inversion of control" or "dependency injection" pattern (for more information Martin Fowler has an informative article at http://www.martinfowler.com/articles/injection.html). The Spring Framework has popularized this pattern, however, the predecessor to Struts2 (WebWork) started using the pattern around the same time.
To understand the inversion of control better, let's look at an example where the processing of the action requires access to the current requests HttpServerRequest object.
你可能首先主要到这个action并没有继承任何的类或者借口。实际上,不仅仅如此。习惯上,action处理请求的函数名字叫做“execute”,但是并不一定要这样做。任何方法只要签名是public String methodName(),都可以通过配置被调用;
其次,返回的对象是String。如果你不喜欢在你的代码当中使用String字面量(不知道理解的对不对),有一个可以在Action当中使用的助手接口,提供了一些公用的result,如"success", "none", "error", "input" and "login" 作为常量。
最后,最具革命性的变化是在Struts2当中处理请求的函数("execute"函数)是没有参数的。那你怎么访问你所需要的对象呢?答案是“反转控制”或“依赖注入”模式(关于这两个模式的详细信息你可一个在Martin Fowler 所写的文章中找到http://www.martinfowler.com/articles/injection.html)。Spring框架推广了这个模式,然而Struts2的前身(WebWork)几乎是在相同的时间也使用了这个模式。
让我们通过一个在Action当中访问HttpServletRequest(这个地方应该是原文写错了吧)对象的例子来更好的理解反转控制:
The dependency injection mechanism used in this example is interface injection. As the name implies, with interface injection there is an interface that needs to be implemented. This interface contains setters, which in turn are used to provide data to the action. In our example we are using the ServletRequestAware interface, here it is:
When we implement this interface, our simple action from above becomes a little more complex - but now we have a HttpServerRequest object to use.
At this point, some alarm bells are probably going off. There are now class level attributes in the action - which, although not thread-safe, is actually okay. In Struts2, an action instance is created for each request. It's not shared and it's discarded after the request has been completed.
There is one last step left, and that is to associate the ServletConfigInterceptor interceptor with this action. This interceptor provides the functionality to obtain the HttpServletRequest and inject it into actions that implement the ServletRequestAware interface. For the moment, don't worry about the details of the configuration - we'll go into much more detail in the next article. The important thing at the moment is to understand that the interceptor and the interface work hand-in-hand to provide the dependency injection to the action.
The benefit to this design is that the action is completely de-coupled from the framework. The action becomes a simple POJO that can also be used outside of the framework. And for those that encourage unit testing, testing a Struts2 action is going to be significantly easier than wrestling to get a Struts action into a StrutsTestCase or a MockStrutsTestCase unit test.
这个时候,警铃已经拉响了。在Action当中有成员变量,是非线程安全的,居然也可以。因为在Struts2当中,会为每个request创建一个Action对象。所以action对象不会在被共享,当请求结束之后,它就会被丢弃掉。
那么最后一个步骤就是将ServletConfigInterceptor拦截器关联到这个action。这个过滤器提供获得HttpServletReques对象t的方法,并将它注入到实现ServletRequestAware接口的Action当中。目前,不用担心配置的细节,我们会在下篇文章中详细介绍。现在的重点是要理解拦截器和接口共同工作为Action提供依赖注入。
这种设计的好处是减弱action对于框架的依赖。action变成了一个简单的POJO,可以再框架之外使用。而其可以鼓励单元测试。Struts2的单元测试相对Struts要简单的多。因为你不需要和StrutsTestCase和MockStrutsTestCase摔跤(我的理解就是StrutsTestCase和MockStrutsTestCase用起来非常的麻烦)
By now you should be familiar with the basics of Struts2 - the high level architecture and basic request workflow. You should be able to configure Struts2 in a servlet container, and know the differences between the action for Struts and Struts2.
In the next article we will introduce the example application that will be migrated, as well as building on the knowledge attained here to migrate the example applications' actions from Struts to Struts2. What we'll end up with is a working application using JSTL, JSP and Struts2. We'll also further investigate the differences between actions; configuration of actions and other framework elements; and talk more about the action related framework features.
现在你应该在整体架构和基本的request工作流畅上熟悉了Struts2.你应该能够将Struts2配置安装到一个Servlet容器上,也应该明白了Struts和Struts2在action方面的差别。
下篇文章我们会介绍需要迁移的样例应用程序,也会通过将应用程序从Struts迁移到Struts2来构建知识。我们也会更加深入的调查Action之间的区别;action以及框架其他元素的配置方法;了解更多的action框架的相关特性(这句话翻译的好像不对),最终我们会得到一个使用JSTL,JSP和Struts2实现的可以运行的应用程序