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

发布于 2024-11-24 00:01:12 字数 813 浏览 0 评论 0原文

我正在使用 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 支持的方法?

I'm using Struts 1.1 (don't get me started), and upgrading is not an option right now. Unfortunately, the whole singleton action approach drives me nuts and makes the action rather complicated since we can't use instance variables and the like, meaning all the necessary objects need to be passed around to all the methods as parameters. I prefer using instance variables, so I'd like to create a new instance of the Action for each request from the Action's execute method, like this:

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
}

This works fine, but I'm wondering if you folks can think of any problems with this approach. I'm aware that this will take a few extra CPU cycles due to the extra object creation, but the wisdom these days is that there's a ton of object creation going on for each request and one more will go unnoticed; not to mention it's worth it to improve readability and maintainability. Are there other disadvantages that are more severe?

Also, is there an easier way to do it than this semi-kludgy approach? Perhaps a Struts-supported approach?

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

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

发布评论

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

评论(1

喜爱皱眉﹌ 2024-12-01 00:01:12

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

在 Struts 应用程序中,大多数业务逻辑都可以使用 JavaBean 来表示。 Action 可以调用 JavaBean 的属性,而无需知道它实际如何工作。这封装了业务逻辑,以便 Action 可以专注于错误处理以及将控制转发到哪里。

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

Struts 1.1 Actions are not intended to do the business logic:

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.

The problem is not that you're creating an extra instance, but that you're creating an extra instance of the action instead of some helper/dao.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文