views:

103

answers:

3

Hi all, 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

A: 

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.

duffymo
hi, so do you think the only way is to reduce the number of actions right? I'd say that I can reduce them, but not so much...most of them are Ajax calls anyway...
Roberto
I'm suggesting that several of those Action classes have a single method. It might be possible to combine several methods into a single class and cut down on the number of classes. If you really need all those calls, then you need them.
duffymo
+2  A: 

You can map a single Action class into several urls each one handled by a different method of the class. Checkout 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).

acerisara
A: 

Hello

If you have hundreds of actions to map you can use wildcard mapping. For example:

book_ad, book_edit, book_new >>can be translated to>> book_*

You can also reuse actions with the redirectAction or redirectChain results, reducing in this way the number of actions.

If you do not remember those topics here a tutorial:

Actions and Results tutorial

Mark