开发者

Any problem with creating a new Struts Action instance from the Action's execute() method?

I'm using Struts 1.1开发者_C百科 (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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜