Isn't this similar to what the Dispatch action does?

-----Original Message-----
From: Chan, Charles [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 13, 2003 10:18 AM
To: 'Struts Users Mailing List'
Subject: One way of writing BaseAction


Hi, I just want to share with you my base action class implementation. I use
reflection to avoid putting <if>..</else> logic in the execute() method. I
am not sure if somone has done this before, so I want to share fragement of
my codes here and ask for opinions.

I have used it to implement a simple site and am quite happy with it.

Let me know what you think or if you find it useful. :)

Cheers
Charles


/**
 * This class is an abstract Struts <code>Action</code> class that all
 * <code>Action</code>  classes should extend. A typical Action class
 * handles multiple actions (requests). For example, a EmailAction class
should
 * be able to handle all actions related to Emails (sending, replying,
viewing,
 * etc.). To avoid cluttering the <code>execute</code> method with
<code>if...
 * else</code> block, this base Action class calls individual
<code>doXXX</code>
 * methods in the derived class to perform the specific action. The exact
name
 * of the method (the XXX) is defined as a <code>parameter</code> in the
Action
 * mapping section of struts- config.xml.
 * <p>
 * An example of struts-config.xml for a EmailAction:
 * <p>
 * Calls EmailAction.doNew() when /email/new.do is requested.
 * <pre>
 * &lt;action path="/email/new"
 *         name="emailForm"
 *         type="EmailAction"
 *         parameter="new"
 *         scope="request"
 *         validate="true"&gt;
 *   &lt;forward name="Success"
 *            path="/jsp/form/emailForm.jsp"
 *            redirect="false" /&gt;
 * &lt;/action&gt;
 * </pre>
 * <p>
 * Calls EmailAction.doView() when /email/view.do is requested.
 * <pre>
 * &lt;action path="/email/view"
 *         name="emailForm"
 *         type="EmailAction"
 *         parameter="view"
 *         scope="request"
 *         validate="true"&gt;
 *   &lt;forward name="Success"
 *            path="/jsp/form/emailForm.jsp"
 *            redirect="false" /&gt;
 * &lt;/action&gt;
 * </pre>
 */

public abstract class BaseAction extends Action
{
    /**
     *  Our default implementation. If parameter is specified in
ActionMapping,
     *  we will do the new processing mode and call the method
     *  "String doParameter()", e.g. if parameter is "edit", "String
doEdit()"
     *  is executed. The return String of that method is used to find the
     *  ActionForward from the mapping.
     *
     *  If no such parameter is found, we'll write some debugging
information
     *  to the response to let the programmer know that he forget to
     *  implement the perform() method.
     */
    public final ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception
    {
        boolean hasForm = (form != null);

        /* Use our custom Request object to protect access to
HttpServletRequest */
        Request req = new Request(request);

        String mode = mapping.getParameter();

        // invoke "doMode() with custom request object (using MethodUtils)
        // based on hasForm, we can either invoke doMode(req) or doMode(req,
form)
    }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to