You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Apache Wiki <wi...@apache.org> on 2006/02/17 00:24:57 UTC

[Struts Wiki] Update of "StrutsCatalogDispatchActionImproved" by MichaelJouravlev

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.

The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/StrutsCatalogDispatchActionImproved

The comment on the change is:
Ophaned; better option: http://issues.apache.org/bugzilla/show_bug.cgi?id=38343

------------------------------------------------------------------------------
- StrutsCatalog: 
+ deleted
  
- '''You can use a dispatch action without requiring configuration of a parameter attribute in struts-config.xml.'''
- 
- 
- '''Assume that you have ''button'' code not unlike:'''
- 
- 
- '''LINKS:'''
- {{{
- <a href='update.do?update.x=update'>UPDATE</a>
- <a href='update.do?update.x=delete'>DELETE</a>
- }}}
- '''FORM SUBMITS:'''
- {{{
- <input type='submit' name='update.x' value='update'>UPDATE
- <input type='submit' name='delete.x' value='delete'>DELETE
- }}}
- '''IMAGES:'''
- {{{
- <input type='image' name='update' src='update.gif'>
- <input type='image' name='delete' src='update.gif'>
- }}}
- 
- '''or the equivalent, in the various Struts' image taglibs:'''
- 
- '''Now, how do we know which image has been clicked or which command has been clicked and, additionally, have the methods automatically generated?  The answer has been complicated and costly in the past.  Here is a simple way to achieve everything at a low cost and with great flexibility and freedom.'''
- {{{
- public abstract class SimpleDispatchAction
- 		extends Action {
-   protected           Class            clazz    = this.getClass();
-   protected static    Log              log      = LogFactory.getLog(SimpleDispatchAction.class);
-   protected static    MessageResources messages = MessageResources.getMessageResources ("org.apache.struts.actions.LocalStrings");
-   protected           HashMap          methods  = new HashMap();
-   protected           Class[]          types    = { ActionMapping.class,
-                                                     ActionForm.class,
-                                                     HttpServletRequest.class,
-                                                     HttpServletResponse.class};
-   public ActionForward execute(ActionMapping mapping,					
-                                ActionForm form,
-                                HttpServletRequest request,
-                                HttpServletResponse response)
-       throws Exception {
-     String name = getMethodName(request);
-     if ("execute".equals(name) || "perform".equals(name)){
-       // Prevent recursive calls
-       String message = messages.getMessage("dispatch.recursive", mapping.getPath());
-       log.error(message);
-       throw new ServletException(message);
-     }
-     return dispatchMethod(mapping, form, request, response, name);
-   }
- 
-   protected ActionForward dispatchMethod(ActionMapping mapping,
-                                          ActionForm form,
-                                          HttpServletRequest request,
- 					 HttpServletResponse response,
-                                          String name)
-       throws Exception {
-     if (name == null) {
-       return this.unspecified(mapping, form, request, response);
-     }
-     Method method = null;
-     try {
-       method = getMethod(name);
-     } catch(NoSuchMethodException nsme) {
-       String message = messages.getMessage("dispatch.method", mapping.getPath(), name);
-       log.error(message, nsme);
-       throw nsme;
-     }
-     ActionForward forward = null;
-     try {
-       Object args[] = {mapping, form, request, response};
-       forward = (ActionForward) method.invoke(this, args);
-     } catch(ClassCastException cce) {
-       String message = messages.getMessage("dispatch.return", mapping.getPath(), name);
-       log.error(message, cce);
-       throw cce;
-     } catch(IllegalAccessException iae) {
-       String message = messages.getMessage("dispatch.error", mapping.getPath(), name);
-       log.error(message, iae);
-       throw iae;
-     } catch(InvocationTargetException ite) {
-       Throwable t = ite.getTargetException();
-       if (t instanceof Exception) {
-         throw ((Exception) t);
-       } else {
-         String message = messages.getMessage("dispatch.error", mapping.getPath(), name);
-         log.error(message, ite);
-         throw new ServletException(t);
-       }
-     }
-     return (forward);
-   }
- 
-   protected static String getMethodName(HttpServletRequest request) {
-     String      command     = null;
-     String      buttonValue = null;
-     Enumeration enum        = request.getParameterNames();
-     while(enum.hasMoreElements()) {
-       buttonValue = (String)enum.nextElement();
-       if(buttonValue.endsWith(".x")) {
-         command = buttonValue.substring(0,buttonValue.indexOf('.'));
-       }
-     }
-     return command;
-   }
- 
-   protected Method getMethod(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 unspecified(ActionMapping mapping,
-                                       ActionForm form,
-                                       HttpServletRequest request,
-                                       HttpServletResponse response)
-       throws Exception {
-     String message = messages.getMessage( "dispatch.parameter", mapping.getPath(), getMethodName(request));
-     log.error(message);
-     throw new ServletException(message);
-   }
- }
- 
- }}}
- 
- '''Use methods like the following in your subclass:'''
- {{{
-   public ActionForward update(ActionMapping mapping,
-                               ActionForm form,
-                               HttpServletRequest request,
-                               HttpServletResponse response)
-       throws IOException, ServletException {
-     // do add
-     return mapping.findForward("success");
-   }
- 
-   public ActionForward delete(ActionMapping mapping,
-                               ActionForm form,
-                               HttpServletRequest request,
-                               HttpServletResponse response)
-       throws IOException, ServletException {
-     // do add
-     return mapping.findForward("success");
-   }
- }}}
- 
- '''So, just mine the value of the [name].x request parameter for a generic solution.'''
- 
- 
- '''Michael !McGrady'''
- 
- 
- 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org