Handling hundreds of actions in Struts2
I've inherited a struts 1 web application where, in order to reduce the number of Action classes (I guess this is the reason), lots of actions are mapped inside a single Action class, like:
public XXXAction() throws Exception{
actions 开发者_如何学编程= new Hashtable();
actions.put("/XXX/main/load", new Integer(0));
actions.put("/XXX/main/save", new Integer(1));
......
}
public ActionForward executeAction(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
try
{
switch (((Integer) actions.get(action)).intValue())
{
case 0:
loadXXXMain();
break;
case 1:
.......
and so on (in some Action classes I have almost one hundred of these small actions).
Now I'm looking at migrate to struts2 and I would like to have a cleaner and better solution to handle this, maybe without having a single Action class for each of these small classes. What do you suggest? I don't like this solution, but I don't like having hundreds of Action classes....
Thanks! Roberto
You can map a single Action class into several urls each one handled by a different method of the class. Check out the Struts2 documentation here. I'm generally used to map a single url to a single Action class, but I think you can also group some urls together if they are related to the same domain concept (like CRUD, for example).
Sounds like you've got a significant refactoring on your hands. I agree that hundreds of fine-grained Actions doesn't sound like a good solution.
Start writing unit tests before you begin. Maybe Selenium can help you here.
精彩评论