从 Struts1 迁移到 Struts2 [英] Migration from Struts1 to Struts2

查看:35
本文介绍了从 Struts1 迁移到 Struts2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Struts1 中有我的应用程序,并且我在所有操作中都使用了 Dispatch 操作.现在请告诉我如何切换到 struts 2 以及应该进行哪些修改以更改我的所有操作和表单 bean.

I have my application in Struts1 and I have used Dispatch action in all my actions. Please tell me now how do I Shift to struts 2 and what are the modifications that should be made to change all my actions and form beans.

推荐答案

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

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:

配置框架

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

The first, and most important configuration, is the one that enables the web application framework within the servlet containers web.xml file.

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

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>

对于 Struts2,几乎没有什么变化.最重要的是调度器已经从一个 servlet 变成了一个 servlet 过滤器.配置就像 servlet 一样简单,如下所示:

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>

分解动作

在请求演练中,我们从高层次讨论了 Struts 和 Struts2 之间的一些差异.现在让我们更深入一步,看看每个框架中动作结构之间的差异.

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.

让我们首先回顾一下 Struts 动作的一般结构.Struts 操作的一般形式如下所示:

Let's first review the general structure of the Struts action. The general form of the Struts action looks like this:

公共类 MyAction 扩展了 Action {公共 ActionForward 执行(ActionMapping 映射,ActionForm 表单,HttpServletRequest 请求,HttpServletResponse 响应)抛出异常{//做工作返回(mapping.findForward(成功"));}}

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")); } }

在实现 Struts 操作时,您需要注意以下事项:

When implementing a Struts action, you need to be aware of the following items:

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

相比之下,Struts2 操作提供了更简单的实现.这是它的样子:

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";
   }
}

您可能已经注意到的第一件事是该操作不扩展任何类或接口.事实上,它比这更进一步.按照惯例,在处理动作时调用的方法是执行"方法——但并非必须如此.任何遵循方法签名 public String methodName() 的方法都可以通过配置调用.

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.

最后,也许与原始 Struts 实现最革命性的区别是,在处理动作时调用的方法(execute"方法)没有参数.那么如何访问需要使用的对象呢?答案在于控制反转"或依赖注入"模式(有关更多信息,Martin Fowler 在 http://www.martinfowler.com/articles/injection.html">http://www.martinfowler.com/articles/injection.html">://www.martinfowler.com/articles/injection.html).Spring Framework 已经普及了这种模式,然而,Struts2 的前身(WebWork)大约在同一时间开始使用这种模式.

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.

这篇关于从 Struts1 迁移到 Struts2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆