gpt4 book ai didi

java - 从 Action 的execute() 方法创建新的 Struts Action 实例有问题吗?

转载 作者:行者123 更新时间:2023-12-01 15:48:13 25 4
gpt4 key购买 nike

我正在使用 Struts 1.1(别让我开始),现在无法升级。不幸的是,整个单例操作方法让我发疯,并使操作变得相当复杂,因为我们不能使用实例变量等,这意味着所有必要的对象都需要作为参数传递给所有方法。我更喜欢使用实例变量,因此我想为来自 Action 的执行方法的每个请求创建一个新的 Action 实例,如下所示:

public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
return (new SaveUserDataAction()).instanceExecute(mapping, form, request, response);
}

public ActionForward instanceExecute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// all normal execute stuff goes here
}

这很好用,但我想知道你们是否能想到这种方法的任何问题。我知道,由于额外的对象创建,这将花费一些额外的 CPU 周期,但目前的智慧是,每个请求都会进行大量的对象创建,而更多的对象创建将被忽视;更不用说提高可读性和可维护性是值得的。还有其他更严重的缺点吗?

此外,还有比这种半笨拙的方法更简单的方法吗?也许是 Struts 支持的方法?

最佳答案

Struts 1.1 Actions无意执行业务逻辑:

In a Struts application, most of the business logic can be represented using JavaBeans. An Action can call the properties of a JavaBean without knowing how it actually works. This encapsulates the business logic, so that the Action can focus on error handling and where to forward control.

问题不在于您正在创建一个额外实例,而是您正在创建一个额外的操作实例,而不是一些 helper/dao。

关于java - 从 Action 的execute() 方法创建新的 Struts Action 实例有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6693070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com