struts2 action class-1 class per (ajax)request?
In my struts 2 web app, there are several menu tabs on a webpage. These menu tabs f开发者_运维百科ire ajax actions when clicked. All these actions come to a single action class. This action class routes the request to a helper class. The helper class has a method that does :
If action = this, do this If action = that, do that And so on….for each action(i.e each tab). Can someone offer comments on this design….is this a correct use of struts 2? Or should we have separate action classes? Also, any standard practices for the helper class?(i.e should it be static, singleton, thread safe etc.)Couple of thoughts:
There's nothing wrong with using an Action class to handle multiple struts2 actions; struts2 allows you to route a particular action to a method of a class. That can be helpful if the class itself is a useful organizing device. For example, you might have a Widget that you want to Create, Read, Update, etc and rather than have a CreateWidgetAction, ReadWidgetAction, etc set of classes that will cause a lot of clutter, it's nicer to simply have a WidgetAction class that has create(), update(), etc methods on it. I am guessing it's less of a nice fit in your case, given what you describe (different menu tabs), but you can certainly do it. Less files to look at while maintaining code.
Note that you will need to map different actions to these different methods as well as different JSPs that render the results.
With regards to the helper class, my advice is to put view + controller related logic in the Action class(es), but leave the model logic in other classes (possibly your helper class). If your helper class is computing data independent of presentation, this is certainly legitimate. But if your helper class is simply preparing a view, put the logic in the Action itself.
精彩评论