Search code examples
javaservletsstruts-1

How to lookup configured Struts action


I want to do the following:

final Action myAction = getActionDefinedInStrutsConfig(param);
myAction.execute(params);

Is there a way to lookup the actions that the ActionServlet has initialized ?

I can create a new one like so:

 final Action myAction = new ActionImpl();
 myAction.execute(params);

but this way the new action is not properly initialized, the attached servlet is not set and getServlet() returns null.


A little clarification on why I need this:

The problem is that I currently have 2 login pages. One for normal users and one for admins. They should be separate systems completely, but the fact is they're currently not. I need to make a 'proxy' login page which decides which login page to redirect to according to the request. If I redirect to the URL however the UI would be drawn. I need to call the either the user or admin login actions to process my proxy page request. Also moving the logic inside a service, while being the correct approach, is not currently an option.


Solution

  • Ok, since I see what you mean here's what I would suggest:

    • Use your action for validatory purposes, i.e., retrieval of data from ActionForm and checking for validity. Once all information is done, send the info to a service.
    • The service (need not to be a web service, but a simple POJO) will have the business logic of the application, with relevant exceptions and return types. Once you call the relevant service with its method, retrieve the result and, finally,
    • Populate your necessary ActionForm or do a mapping.findForward().

    This way, if you need another business logic that is used by another Struts Action, rather call the service that the 2nd action uses. This is an effective way for code-reuse and good OOP practise.

    Hope this helps.


    The hackable way would be to do this:

    final Action myAction = new ActionImpl();
    myAction.setServlet(getServlet());
    /* ONLY if your form enctype is "multipart/request-data". */
    myAction.setMultipartRequestHandler(getMultipartRequestHandler());
    
    //Finally
    myAction.execute(params);