I made something along the same lines, but not with
reflection.  I've attached the file.

David

--- DONNIE HALE <[EMAIL PROTECTED]> wrote:
> Craig,
> 
> Last week, there was a thread labelled: "Minimizing
> Action class proliferation". I think you were on
> your brief "no list access" hiatus. :)
> 
> Below is the long-and-short of a proposed
> enhancement to Struts that could optionally be used
> by developers if they are so inclined. The only
> piece missing from this is a proposed change to the
> "action" XML elemen to include a "handler" or
> "method" attribute. This would be used as the first
> parameter to the "class.getMethod" call in the code
> below.
> 
> I won't regurgitate the whole thread here. I was
> just wondering if you had any thoughts on this?
> 
> Thanks,
> 
> Donnie
> 
> 
> >>> [EMAIL PROTECTED] 03/07/01 10:05PM
> >>>
> Donnie,
> 
> How about this as an alternative. I have created a
> subclass of Action called
> StandardAction which uses reflection to invoke a
> method with the same name
> as the "actions" path (defined in the
> struts-config.xml file). So all you
> have to do is extend the Standard action and
> implement methods that
> correspond to the paths that use it.
> 
> For example if you define three actions in your
> struts-config.xml file of
> /saveOrder, /editOrder and /deleteOrder that all use
> a class OrderAction
> which extends StandardAction and then create a
> OrderAction class as shown
> below:
> 
> I hope this is of use.
> 
> Niall
> 
> 
> --------------Example of StandardAction
> implementation-----------
> 
> public class OrderAction extends StandardAction{
> 
>   public ActionForward saveOrder(ActionMapping
> mapping,
>                                ActionForm form,
>                                HttpServletRequest
> request,
>                                HttpServletResponse
> response) {
> 
> 
>   }
>   public ActionForward editOrder(ActionMapping
> mapping,
>                                ActionForm form,
>                                HttpServletRequest
> request,
>                                HttpServletResponse
> response) {
> 
> 
>   }
>   public ActionForward deleteOrder(ActionMapping
> mapping,
>                                ActionForm form,
>                                HttpServletRequest
> request,
>                                HttpServletResponse
> response) {
> 
> 
>   }
> }
> 
> -------------------------------StandardAction
> Class-----------
> package nkp;
> 
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> import javax.servlet.http.HttpSession;
> 
> import org.apache.struts.action.Action;
> import org.apache.struts.action.ActionForm;
> import org.apache.struts.action.ActionForward;
> import org.apache.struts.action.ActionMapping;
> import java.lang.reflect.Method;
> import java.lang.reflect.InvocationTargetException;
> 
> /**
>  * @author Niall Pemberton
>  * @version 1.0
>  */
> 
> public abstract class StandardAction  extends Action
> {
> 
>   public ActionForward perform(ActionMapping
> mapping,
>                                ActionForm form,
>                                HttpServletRequest
> request,
>                                HttpServletResponse
> response) {
> 
> 
>     // Determine the method name to call (based on
> the path without the "/")
>     String methodName =
> mapping.getPath().substring(1);
> 
>     // Get the method with the same name as the path
>     Class cls = this.getClass();
>     Method method = null;
>     try {
>       method = cls.getMethod(methodName, new Class[]
> {ActionMapping.class,
>                                                     
>  ActionForm.class,
> 
> HttpServletRequest.class,
> 
> HttpServletResponse.class});
>     }
>     catch (NoSuchMethodException ex) {
>       servlet.log("Method "+methodName+" not found
> in class
> "+this.toString());
>     }
> 
>     // Invoke the Method
>     Object obj = null;
>     try {
>       obj = method.invoke(this, new Object[]
> {mapping, form, request,
> response});
>     }
>     catch (InvocationTargetException etargEx) {
>       servlet.log("Error Involing Method
> "+methodName+"(InvocationTargetException)
> "+this.toString());
>     }
>     catch (IllegalAccessException illEx) {
>       servlet.log("Error Involing Method
> "+methodName+"(IllegalAccessException)
> "+this.toString());
>     }
>     ActionForward actionForward =
> (ActionForward)obj;
> 
>     // Forward control to the specified success URI
>     return actionForward;
>   }
> }


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

GenericAction.java

Reply via email to