从 Action 的execute() 方法创建新的 Struts Action 实例有问题吗?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Struts 1.1 Actions 无意执行业务逻辑:
问题不在于您正在创建一个额外实例,而是您正在创建一个额外的操作实例而不是某些 helper/dao。
Struts 1.1 Actions are not intended to do the business logic:
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.