RE: One way of writing BaseAction

2003-01-13 Thread Chan, Charles
Yes, I think you're right! :) That's the good thing about posting ideas to
mailing list. I probably will still go with my approach instead of using
DispatchAction because:

1. I don't need to specify a method parameter in request URL.
2. I can pass my custom Request object to protect HttpServletRequest.

Thanks,
Charles


-Original Message-
From: Siggelkow, Bill [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 13, 2003 12:50 PM
To: 'Struts Users Mailing List'
Subject: RE: One way of writing BaseAction


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 .. 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 Action class that all
 * Action  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 execute method with
if...
 * else block, this base Action class calls individual
doXXX
 * methods in the derived class to perform the specific action. The exact
name
 * of the method (the XXX) is defined as a parameter in the
Action
 * mapping section of struts- config.xml.
 * 
 * An example of struts-config.xml for a EmailAction:
 * 
 * Calls EmailAction.doNew() when /email/new.do is requested.
 * 
 * <action path="/email/new"
 * name="emailForm"
 * type="EmailAction"
 * parameter="new"
 * scope="request"
 * validate="true">
 *   <forward name="Success"
 *path="/jsp/form/emailForm.jsp"
 *redirect="false" />
 * </action>
 * 
 * 
 * Calls EmailAction.doView() when /email/view.do is requested.
 * 
 * <action path="/email/view"
 * name="emailForm"
 * type="EmailAction"
 * parameter="view"
 * scope="request"
 * validate="true">
 *   <forward name="Success"
 *path="/jsp/form/emailForm.jsp"
 *redirect="false" />
 * </action>
 * 
 */

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]>

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




One way of writing BaseAction

2003-01-13 Thread Chan, Charles
Hi, I just want to share with you my base action class implementation. I use
reflection to avoid putting .. 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 Action class that all
 * Action  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 execute method with
if...
 * else block, this base Action class calls individual
doXXX
 * methods in the derived class to perform the specific action. The exact
name
 * of the method (the XXX) is defined as a parameter in the
Action
 * mapping section of struts- config.xml.
 * 
 * An example of struts-config.xml for a EmailAction:
 * 
 * Calls EmailAction.doNew() when /email/new.do is requested.
 * 
 * 
 *   
 * 
 * 
 * 
 * Calls EmailAction.doView() when /email/view.do is requested.
 * 
 * 
 *   
 * 
 * 
 */

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:   
For additional commands, e-mail: