You can do the same thing WITHOUT the parameter values in the
struts-config.xml and the following code:

<html:form action="/user">
<html:submit property="new.method"><bean:message
key="button.new"/></html:submit>
<html:link page="/user.do?new.method=''">New</html:link>
</html:form>

if you what the method to be "new(ActionMapping mapping, ActionForm
form, HttpServletRequest request, HttpServletResponse response) and
you use the following class (with logging changes) instead of
LookupDispatchAction:


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 Sat, 15 Jan 2005 03:01:45 -0600, Kishore Senji <[EMAIL PROTECTED]> wrote:
> On Fri, 14 Jan 2005 18:01:25 -0200, Flávio Maldonado
> <[EMAIL PROTECTED]> wrote:
> > Hello...
> >
> > When I use this tags above, the Button works well, but the Link doesn't
> > work.
> >
> > <html:form action="/user">
> > <html:submit property="action"><bean:message
> > key="button.new"/></html:submit>
> > <html:link page="/user.do?action='button.new'">New</html:link>
> 
> Try the following code below
> 
> <%
>     String message =
> org.apache.struts.taglib.TagUtils.getInstance().message(pageContext,
>     org.apache.struts.Globals.MESSAGES_KEY,
>     org.apache.struts.Globals.LOCALE_KEY,
>     "button.new");
>     pageContext.setAttribute("message", message);
> %>
> 
> <html:link action="/user" paramId="action" paramName="message">New</html:link>
> 
> With a LookupDispatchAction you should send the value for a key in the
> ApplicationResources.properties file and not the key as a request
> parameter
> 
> > </html:form>
> >
> > At the first moment, both are putting the "button.new" into the action
> > variable.
> > When I press the button, works well. But I need that the link do the same
> > function of the button but I receive this error message when I try click on
> > the link:
> >
> > java.lang.NullPointerException
> >        java.lang.Class.searchMethods(Class.java:1877)
> >        java.lang.Class.getMethod0(Class.java:1901)
> >        java.lang.Class.getMethod(Class.java:984)
> >        
> > org.apache.struts.actions.DispatchAction.getMethod(DispatchAction.java:332)
> >        
> > org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:264)
> >        
> > org.apache.struts.actions.LookupDispatchAction.execute(LookupDispatchAction.java:234)
> >        
> > org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:465)
> >        
> > org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
> >        
> > org.apache.struts.action.ActionServlet.process(ActionServlet.java:1422)
> >        org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:505)
> >        javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
> >        javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
> >
> > I extend my Action from LookupDispatchAction, and I implement this method...
> >
> >    protected Map getKeyMethodMap() {
> >        Map map = new HashMap();
> >        map.put("button.new", "newUser");
> >        return map;
> >    }
> >
> > With this mapping, I hope that the method above be executed...
> >
> > public ActionForward newUser(ActionMapping mapping, ActionForm form,
> > HttpServletRequest request, HttpServletResponse response){
> >   return mapping.getInputForward();
> > }
> >
> > but it's not happening...
> >
> > Some body can Help-me?
> >
> > I'm sorry for my bad english... ;)
> >
> > Thanks...
> >
> > Flávio Vilasboas Maldonado
> > Diretor de Desenvolvimento
> > SedNet Soluções em TI
> > (35)3471-9381
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


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

"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