On 4/19/05, Joe Germuska <[EMAIL PROTECTED]> wrote:
> At 11:06 PM -0700 4/18/05, Michael J. wrote:
> >2) You can set the params too, it is not a big deal. Do not edit
> >existing ActionForward from findForward, instead create a new one and
> >append query parameters.
> 
> Note that about two months ago, a class called "ActionRedirect" was
> added to clean up this particular use case (adding parameters to a
> redirecting ActionConfig.)
> 
> http://cvs.apache.org/viewcvs.cgi/struts/core/trunk/src/share/org/apache/struts/action/ActionRedirect.java?root=Apache-SVN&rev=153901&view=markup
> 
> No Struts release has been made since this was added, but you should
> know it's there and if you're using a nightly build, you should use
> it.
> 
> Joe

Whoa, a whole class. I use just a simple method, which appends
parameters for redirected request. There is no point to do this for
forwarded parameters, they can be passed through server-side objects.

This utility method is quite simple, it does not do a lot of error
handling, nor does it check for character set or for maximum URL
length. I use the same mappings from struts-config.xml file, I just
append request query parameters.

This is my action class:
------------------------

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) {
 
  ...

  return ActionTools.goForward(
    mapping,                     // mapping of this action
    "editItem",                  // edit action name: "editItem.do"
    true,                        // redirect == true
    new String[] {"id="+itemId}  // ex: "id=1234"
  );
}

This is my helper method:
-------------------------

/**
 * Updates ActionForward object with URL parameters.
 * @param actionMapping  action mapping object
 * @param forwardName    mapping name
 * @param redirect       true if redirect, false if forward
 * @param urlParams      array of "key=value" strings which
 *                       should be added to actionForward path
 *                       as HTTP GET parameters
 * @return modified ActionForward object with updated GET parameters
 */
public static ActionForward goForward(ActionMapping actionMapping,
                                      String forwardName,
                                      String[] urlParams)
{
  // Find ActionForward object, defined in struts-config.xml
  ActionForward actionForward = actionMapping.findForward(forwardName);
  if (actionForward == null) return null;

  // Append URL parameters. Important on redirect.
  String actionPath = actionForward.getPath();
  if (actionPath != null && urlParams != null) {
    for (int i = 0; i < urlParams.length; i++) {
      actionPath += i==0 ? "?" : "&";
      actionPath += urlParams[i];
    }
  }

  // Create new ActionForward object. Stuts does not
  // allow to modify ActionForward objects, statically
  // defined in struts-config.xml
  ActionForward actionRedirect =
    new ActionForward(actionForward.getName(),
                      actionPath,
                      redirect
    );

  return actionRedirect;
}

Michael.

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

Reply via email to