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:
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:
public class MyAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // do the work return (mapping.findForward("success")); } }
When implementing a Struts action, you need to be aware of the following items:
All actions have to extend the Action base class.
All actions have to be thread-safe, as only a single action instance is created.
Because the actions have to be thread-safe, all the objects that may be needed in the processing of the action are passed in the method signature.
The name of the method that is invoked for the processing of the action is "execute" (there is a DispatchAction class available in Struts which can re-route the method to be executed to another method in the same action, however the initial entry point from the framework into the action is still the "execute" method).
An ActionForward result is returned using a method from the ActionMapping class, most commonly via the "findForward" method call.
In contrast, the Struts2 action provides a much simpler implementation. Here's what it looks like:
public class MyAction {
public String execute() throws Exception {
// do the work
return "success";
}
}
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.
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.
You may use dynamic method invocation to vaguely-mimic the old DispatchAction-style "pass in the method name" functionality (or you could write an interceptor that used a parameter to do much the same thing).
There are no "form beans" per se in Struts 2, although you can implement ModelDriven (some docs) and it sort-of works like a form bean.
All JSPs will need to be re-written, but only if you're using the Struts 1 tags. If you used only JSTL tags, you may not need to, it'd depend.
The link provided by Ischin is a good place to start getting more details.
发布评论
评论(3)
我会向您推荐这个文档系列:
第一个链接解释主题,第二个链接中有一个示例。我在下面写了一个摘自那里的解释:
配置框架
第一个也是最重要的配置是在 servlet 容器 web.xml 文件中启用 Web 应用程序框架的配置。
大家应该熟悉的 Struts 配置是:
对于 Struts2 来说变化很少。最重要的是调度程序已从 servlet 更改为 servlet 过滤器。配置与 Servlet 一样简单,如下所示:
解构操作
在请求演练中,我们从较高的层次讨论了 Struts 和 Struts2 之间的一些差异。现在让我们更深入地了解一下每个框架中操作结构之间的差异。
我们首先回顾一下Struts Action的总体结构。 Struts 操作的一般形式如下所示:
public class MyAction extends Action {
公共 ActionForward 执行(ActionMapping 映射,
ActionForm 表单,
HttpServletRequest 请求,
HttpServletResponse 响应)
抛出异常{
// 做工作
return (mapping.findForward("成功"));
}
实现 Struts操作
时,您需要注意以下事项:
相比之下,Struts2 操作提供了更简单的实现。它看起来像这样:
您可能注意到的第一件事是该操作不扩展任何类或接口。事实上,事情远不止于此。按照惯例,在处理操作时调用的方法是“执行”方法 - 但不一定如此。任何遵循方法签名 public String methodName() 的方法都可以通过配置调用。
最后,也许与原始 Struts 实现最具革命性的区别是,在操作处理中调用的方法(“execute”方法)没有参数。那么如何访问您需要使用的对象呢?答案在于“控制反转”或“依赖注入”模式(有关更多信息,Martin Fowler 在 http 上发表了一篇内容丰富的文章://www.martinfowler.com/articles/injection.html)。 Spring 框架普及了这种模式,然而,Struts2 的前身(WebWork)大约在同一时间开始使用该模式。
I will suggest you this document series:
First link explains the topic and there is an example at second link. I wrote below an explanation taken from there:
Configuring the framework
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:
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:
Deconstructing the Actions
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:
public class MyAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// do the work
return (mapping.findForward("success"));
}
}
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:
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.
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.
您可以使用动态方法调用 模糊地模仿旧的
DispatchAction
风格的“传递方法名称”功能(或者您可以编写一个使用参数来执行相同操作的拦截器)。尽管您可以实现 ModelDriven (一些文档)并且它排序-of像表单bean一样工作。
所有 JSP 都需要重写,但前提是您使用 Struts 1 标记。如果您仅使用 JSTL 标记,则可能不需要,这取决于情况。
Ischin 提供的链接是开始获取更多详细信息的好地方。
You may use dynamic method invocation to vaguely-mimic the old
DispatchAction
-style "pass in the method name" functionality (or you could write an interceptor that used a parameter to do much the same thing).There are no "form beans" per se in Struts 2, although you can implement
ModelDriven
(some docs) and it sort-of works like a form bean.All JSPs will need to be re-written, but only if you're using the Struts 1 tags. If you used only JSTL tags, you may not need to, it'd depend.
The link provided by Ischin is a good place to start getting more details.
*.action
*.do .
*.action
*.do .