Oops, try this!

public abstract class StateBaseAction
    extends Action {
  private static final String   DISPATCH_METHOD_SUFFIX = ".method";
  private static final String   EXECUTE                = "execute";
  private static final String   PERFORM                = "perform";
  private static final String   DISPATCH_RECURSIVE     = "dispatch.recursive";
  private static final String   DISPATCH_METHOD        = "dispatch.method";
  protected            HashMap  methods                = new HashMap();
  protected            Class [] types                  = { ActionMapping.class,
                                                           ActionForm.class,
                                                          
HttpServletRequest.class,
                                                          
HttpServletResponse.class };

  public abstract ActionForward execute(ActionMapping       mapping,
                                        ActionForm          form,
                                        HttpServletRequest  request,
                                        HttpServletResponse response)
      throws IOException,
             ServletException,
             NoSuchMethodException;

  protected void saveMessages(HttpServletRequest request,
                              ActionMessages     errors) {
    if ((errors == null) || errors.isEmpty()) {
      request.removeAttribute(Globals.ERROR_KEY);
      return;
    }
    request.setAttribute(Globals.ERROR_KEY, errors);
  }

  public String getMessage(HttpServletRequest  request,
                           String              key)
      throws IOException,
             ServletException {
    HttpSession      session  = request.getSession();
    Locale           locale   =
(Locale)session.getAttribute(Globals.LOCALE_KEY);
    MessageResources messages = this.getResources(request);
    return messages.getMessage(locale,key);
  }

  protected ActionForward method(Action action,
                                 ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
      throws Exception {
    Class  clazz      = action.getClass();
    String methodName = getMethodName(request,mapping);

    if (EXECUTE.equals(methodName) || PERFORM.equals(methodName)){
      MessageResources messages = MessageResources.getMessageResources
("org.apache.struts.actions.LocalStrings");
      String message = messages.getMessage(DISPATCH_RECURSIVE,
mapping.getPath());
      throw new ServletException(message);
    }

    Method method = null;

    try {
      method = getMethod(clazz,methodName);
    } catch(NoSuchMethodException nsme) {
      MessageResources messages = MessageResources.getMessageResources
("org.apache.struts.actions.LocalStrings");
      String message = messages.getMessage(DISPATCH_METHOD,
mapping.getPath(), methodName);
      throw nsme;
    }

    ActionForward forward = null;

    try {
      Object args[] = { mapping, form, request, response };
      forward = (ActionForward)method.invoke(action, args);
    } catch(ClassCastException cce) {
      StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction error 108 = "
+ cce.getMessage());
    } catch(IllegalAccessException iae) {
      StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction errorn 110 =
" + iae.getMessage());
    } catch(InvocationTargetException ite) {
      Throwable t = ite.getTargetException();

      if (t instanceof Exception) {
        StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction error 115 =
" + ((Exception)t).getMessage());
      } else {
        StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction error 117 =
" + ite.getMessage());
      }
    }
    return forward;
  }

  private static String getMethodName(HttpServletRequest request,
                                      ActionMapping      mapping) {

    String methodName  = null;
    String buttonValue = null;
    String paramProperty = mapping.getParameter();
    if((paramProperty != null)) {
      int index = paramProperty.indexOf('.');
      if(index > -1) {
        methodName = paramProperty.substring(0,index);
      } else {
        return methodName = paramProperty;
      }
    }

    Enumeration enum = request.getParameterNames();
    while(enum.hasMoreElements()) {
      buttonValue = (String)enum.nextElement();
      if(buttonValue.indexOf(DISPATCH_METHOD_SUFFIX) >= 0) {
        methodName = buttonValue;
        break;
      }
    }

    if(methodName == null) {
      return null;
    }

    return methodName.substring(0,methodName.indexOf('.'));
  }

  private Method getMethod(Class  clazz,
                             String name)
      throws NoSuchMethodException {
    synchronized(methods) {
      Method method = (Method) methods.get(name);

      if (method == null) {
        method = clazz.getMethod(name, types);
        methods.put(name, method);
      }

      return (method);
    }
  }

  protected ActionForward cancelled(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
      throws Exception {
    return null;
  }
}

Jack


On Fri, 14 Jan 2005 10:10:21 -0800, Dakota Jack <[EMAIL PROTECTED]> wrote:
> Hi, Todd,
> 
> Check out: http://www.michaelmcgrady.com/button/.  The DispatchAction
> there is unlike the one here.  I use something close but a little
> different.  Still, all you have to do is to give the property (name in
> HTML) the suffix you specifiy as the DISPATCH_METHOD_SUFFIX (see
> below).  I use ".method".  NOTE THAT THIS LEAVES YOU FREE TO SPECIFY
> WHATEVER YOU WANT AS THE parameter in the action mapping and as the
> value of the property in the submit, button, etc.  This gives you all
> sorts of cool things you can do.
> 
> I use:
> 
> public abstract class StateBaseAction
>     extends Action {
>   private static final String   DISPATCH_METHOD_SUFFIX = ".method";
>   private static final String   EXECUTE                = "execute";
>   private static final String   PERFORM                = "perform";
>   private static final String   DISPATCH_RECURSIVE     = "dispatch.recursive";
>   private static final String   DISPATCH_METHOD        = "dispatch.method";
>   protected            HashMap  methods                = new HashMap();
>   protected            Class [] types                  = { 
> ActionMapping.class,
>                                                            ActionForm.class,
> 
> HttpServletRequest.class,
> 
> HttpServletResponse.class };
> 
>   public abstract ActionForward execute(ActionMapping       mapping,
>                                         ActionForm          form,
>                                         HttpServletRequest  request,
>                                         HttpServletResponse response)
>       throws IOException,
>              ServletException,
>              NoSuchMethodException;
> 
>   protected void saveMessages(HttpServletRequest request,
>                               ActionMessages     errors) {
>     if ((errors == null) || errors.isEmpty()) {
>       request.removeAttribute(Globals.ERROR_KEY);
>       return;
>     }
>     request.setAttribute(Globals.ERROR_KEY, errors);
>   }
> 
>   public String getMessage(HttpServletRequest  request,
>                            String              key)
>       throws IOException,
>              ServletException {
>     HttpSession      session  = request.getSession();
>     Locale           locale   =
> (Locale)session.getAttribute(Globals.LOCALE_KEY);
>     MessageResources messages = this.getResources(request);
>     return messages.getMessage(locale,key);
>   }
> 
>   protected ActionForward method(Action action,
>                                  ActionMapping mapping,
>                                  ActionForm form,
>                                  HttpServletRequest request,
>                                  HttpServletResponse response)
>       throws Exception {
>     Class  clazz      = action.getClass();
>     String methodName = getMethodName(request,mapping);
> 
>     if(methodName == null) {
>       unspecified(mapping,form,request,response);
>     } else if (EXECUTE.equals(methodName) || PERFORM.equals(methodName)){
>       MessageResources messages = MessageResources.getMessageResources
> ("org.apache.struts.actions.LocalStrings");
>       String message = messages.getMessage(DISPATCH_RECURSIVE,
> mapping.getPath());
>       throw new ServletException(message);
>     }
> 
>     Method method = null;
> 
>     try {
>       method = getMethod(clazz,methodName);
>     } catch(NoSuchMethodException nsme) {
>       MessageResources messages = MessageResources.getMessageResources
> ("org.apache.struts.actions.LocalStrings");
>       String message = messages.getMessage(DISPATCH_METHOD,
> mapping.getPath(), methodName);
>       throw nsme;
>     }
> 
>     ActionForward forward = null;
> 
>     try {
>       Object args[] = { mapping, form, request, response };
>       forward = (ActionForward)method.invoke(action, args);
>     } catch(ClassCastException cce) {
>       StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction error 108 = "
> + cce.getMessage());
>     } catch(IllegalAccessException iae) {
>       StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction errorn 110 =
> " + iae.getMessage());
>     } catch(InvocationTargetException ite) {
>       Throwable t = ite.getTargetException();
> 
>       if (t instanceof Exception) {
>         StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction error 115 =
> " + ((Exception)t).getMessage());
>       } else {
>         StdOut.log(SiteConstant.ERROR_LOG,"StateBaseAction error 117 =
> " + ite.getMessage());
>       }
>     }
>     return forward;
>   }
> 
>   private static String getMethodName(HttpServletRequest request,
>                                       ActionMapping      mapping) {
> 
>     String methodName  = null;
>     String buttonValue = null;
>     String paramProperty = mapping.getParameter();
>     if((paramProperty != null)) {
>       int index = paramProperty.indexOf('.');
>       if(index > -1) {
>         methodName = paramProperty.substring(0,index);
>       } else {
>         return methodName = paramProperty;
>       }
>     }
> 
>     Enumeration enum = request.getParameterNames();
>     while(enum.hasMoreElements()) {
>       buttonValue = (String)enum.nextElement();
>       if(buttonValue.indexOf(METHOD_SUFFIX) >= 0) {
>         methodName = buttonValue;
>         break;
>       }
>     }
> 
>     if(methodName == null) {
>       return null;
>     }
> 
>     return methodName.substring(0,methodName.indexOf('.'));
>   }
> 
>   private Method getMethod(Class  clazz,
>                              String name)
>       throws NoSuchMethodException {
>     synchronized(methods) {
>       Method method = (Method) methods.get(name);
> 
>       if (method == null) {
>         method = clazz.getMethod(name, types);
>         methods.put(name, method);
>       }
> 
>       return (method);
>     }
>   }
> 
>   protected ActionForward cancelled(ActionMapping mapping,
>                                  ActionForm form,
>                                  HttpServletRequest request,
>                                  HttpServletResponse response)
>       throws Exception {
>     return null;
>   }
> }
> 
> Jack
> 
> <snip>
> On Fri, 14 Jan 2005 07:35:46 -0500, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> wrote:
> >
> > Jack,
> >       You have said that LookupDispatchAction is outdated and inefficient.  
> > I
> > have at a minimum 6 different actions that need to be performed per page, 
> > not
> > including load.  What else would you recommend as a solution?
> >
> > Todd
> </snip>
> 
> 
> --
> ------------------------------
> 
> "You can lead a horse to water but you cannot make it float on its back."
> 
> ~Dakota Jack~
> 
> "You can't wake a person who is pretending to be asleep."
> 
> ~Native Proverb~
> 
> "Each man is good in His sight. It is not necessary for eagles to be crows."
> 
> ~Hunkesni (Sitting Bull), Hunkpapa Sioux~
> 
> -----------------------------------------------
> 
> "This message may contain confidential and/or privileged information.
> If you are not the addressee or authorized to receive this for the
> addressee, you must not use, copy, disclose, or take any action based
> on this message or any information herein. If you have received this
> message in error, please advise the sender immediately by reply e-mail
> and delete this message. Thank you for your cooperation."
> 


-- 
------------------------------

"You can lead a horse to water but you cannot make it float on its back."

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

-----------------------------------------------

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

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

Reply via email to