从 Struts1 迁移到 Struts2

发布于 2024-12-10 13:49:57 字数 1435 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-12-17 13:49:57

我会向您推荐这个文档系列:

第一个链接解释主题,第二个链接中有一个示例。我在下面写了一个摘自那里的解释:

配置框架

第一个也是最重要的配置是在 servlet 容器 web.xml 文件中启用 Web 应用程序框架的配置。

大家应该熟悉的 Struts 配置是:

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

对于 Struts2 来说变化很少。最重要的是调度程序已从 servlet 更改为 servlet 过滤器。配置与 Servlet 一样简单,如下所示:

<filter>
<filter-name>webwork</filter-name>
<filter-class>
    org.apache.struts.action2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

解构操作

在请求演练中,我们从较高的层次讨论了 Struts 和 Struts2 之间的一些差异。现在让我们更深入地了解一下每个框架中操作结构之间的差异。

我们首先回顾一下Struts Action的总体结构。 Struts 操作的一般形式如下所示:

public class MyAction extends Action {
公共 ActionForward 执行(ActionMapping 映射,
ActionForm 表单,
HttpServletRequest 请求,
HttpServletResponse 响应)
抛出异常{
// 做工作
return (mapping.findForward("成功"));
}
实现 Struts操作

时,您需要注意以下事项:

  • 所有操作都必须扩展 Action 基类。
  • 所有操作都必须是线程安全的,因为只创建一个操作实例。
  • 由于操作必须是线程安全的,因此操作处理中可能需要的所有对象都在方法签名中传递。
  • 处理动作时调用的方法的名称是“execute”(Struts中有一个DispatchAction类,它可以将要执行的方法重新路由到同一动作中的另一个方法,但是初始入口点从框架进入动作仍然是“execute”方法)。
  • ActionForward 结果是使用 ActionMapping 类中的方法返回的,最常见的是通过“findForward”方法调用。

相比之下,Struts2 操作提供了更简单的实现。它看起来像这样:

public class MyAction {
   public String execute() throws Exception {
        // do the work
        return "success";
   }
}

您可能注意到的第一件事是该操作不扩展任何类或接口。事实上,事情远不止于此。按照惯例,在处理操作时调用的方法是“执行”方法 - 但不一定如此。任何遵循方法签名 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:

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

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:

<filter>
<filter-name>webwork</filter-name>
<filter-class>
    org.apache.struts.action2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

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:

  • 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.

快乐很简单 2024-12-17 13:49:57

可以使用动态方法调用 模糊地模仿旧的 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.

情魔剑神 2024-12-17 13:49:57
  • 对于 Struts 2.x:将调度程序操作过滤器设置为 *.action
  • 对于 struts 1.x:将操作过滤器设置为 *.do .
  • For the Struts 2.x: Make the dispatcher action filter as *.action
  • For the struts 1.x: Make the action filter as *.do .
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文