You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by me...@bpam.fr on 2003/05/02 14:53:26 UTC

action design question

hi all,

I have common logic that I would like all my
action class to implement.
The basic solution is to create a BaseAction class that extends struts 
Action.
The problem is that I use both struts Action and Dispatch Action class.

How could I simply and fairly design this ?

thankx

Meissa

Re: action design question

Posted by Kris Schneider <kr...@dotech.com>.
A couple of things to consider:

If you're using a Servlet 2.3 container, the common logic might be appropriate
for a Filter.

If you're not using a Servlet 2.3 container, the common logic might be
appropriate for a custom RequestProcessor. For example by overriding
processPreprocess.

Otherwise, as David suggests, factor out the common logic into a utility class
and then have two base classes that extend Action and DispatchAction and use the
utility class as a helper.

If none of those do it for you, I guess you could do something like:

public abstract class BaseAction extends DispatchAction {

  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
    throws Exception {

    // Identify the request parameter containing the method name
    String parameter = mapping.getParameter();
    if (parameter == null) {
      // default behavior was to generate an error, invoke a method instead
      return doExecute(mapping, form, request, response);
    }

    // Identify the method name to be dispatched to.
    // dispatchMethod() will call unspecified() if name is null
    String name = request.getParameter(parameter);

    // Invoke the named method, and return the result
    return dispatchMethod(mapping, form, request, response, name);
  }

  // override this in subclasses where dispatching is not used
  public ActionForward doExecute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    throws Exception {

    // default error behavior
    String message = messages.getMessage("dispatch.handler", mapping.getPath());
    log.error(message);
    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                       message);
    return (null);
  }
}


Quoting meissa.Sakho@bpam.fr:

> hi all,
> 
> I have common logic that I would like all my
> action class to implement.
> The basic solution is to create a BaseAction class that extends struts 
> Action.
> The problem is that I use both struts Action and Dispatch Action class.
> 
> How could I simply and fairly design this ?
> 
> thankx
> 
> Meissa


-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: action design question

Posted by Navjot Singh <na...@net4india.net>.
there are lot many posts on this topic in the recent archives.
you will definitely find an answer -- you can or CANNOT do it

navjot

|-----Original Message-----
|From: meissa.Sakho@bpam.fr [mailto:meissa.Sakho@bpam.fr]
|Sent: Friday, May 02, 2003 6:23 PM
|To: struts-user@jakarta.apache.org
|Subject: action design question
|
|
|hi all,
|
|I have common logic that I would like all my
|action class to implement.
|The basic solution is to create a BaseAction class that extends struts 
|Action.
|The problem is that I use both struts Action and Dispatch Action class.
|
|How could I simply and fairly design this ?
|
|thankx
|
|Meissa

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org