You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by DONNIE HALE <DH...@longaberger.com> on 2001/03/16 16:22:19 UTC

Craig M. - Comments on "Minimizing Action class proliferation"

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


>>> niall.pemberton@btInternet.com 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;
  }
}

Re: Craig M. - Comments on "Minimizing Action class proliferation"

Posted by John Wright <jw...@fyidocs.com>.
I also created something similar based on a Javaworld article.  My Action
class takes a url  like /objectX/methodX and lookes up in application scope
an object called objectX and invokes the  method called methodX (via
reflection as below).  The method signature is the same as that for perform
except I pass the reference to the Action class in case the class needs
access to certain data that only the Action class holds.  In my Action class
perform method I can then do perform certain global rules like checking for
login, implementing some common routing algorithm, etc..., and I don't have
all those Action classes.  I call these other classes controllers and they
are seperated by business functionality (e.g. AccountWebControl,ler,
MetadataWebController etc..)..  The in turn access a web-neutral JDBC layer
(I don't use EJBs).

If anyone is interested in the code let me know.

John Wright


----- Original Message -----
From: "DONNIE HALE" <DH...@longaberger.com>
To: <st...@jakarta.apache.org>
Sent: Friday, March 16, 2001 9:22 AM
Subject: Craig M. - Comments on "Minimizing Action class proliferation"


> 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
>
>
> >>> niall.pemberton@btInternet.com 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;
>   }
> }
>


Re: Craig M. - Comments on "Minimizing Action class proliferation"

Posted by David Winterfeldt <dw...@yahoo.com>.
I made something along the same lines, but not with
reflection.  I've attached the file.

David

--- DONNIE HALE <DH...@longaberger.com> 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
> 
> 
> >>> niall.pemberton@btInternet.com 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/