You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nic Werner <we...@sonoma.edu> on 2005/04/19 06:35:13 UTC

Using validate() and returning to original page dynamically

Hi again,
    I'm calling the validate() method in my MappingDispatchAction to 
check errors on page. Which is fine, except that I want to return to the 
URL (Action w/params) that was called. I have two Actions, Edit and Add, 
which both refer to Attributes.jsp, and depending on the type param set, 
this page forwards to either Update or Insert, respectively. Therefore, 
setting 'input='Attributes.jsp' won't work as the bean isn't setup 
properly for the Edit Action.
    So, the flow is that I want '/Edit.do?Id=32' to check for errors, 
and if there is one, return to '/Edit.do?Id=32', not Attributes.jsp. It 
isn't enough to set the type (Edit/Add) in the session scope, I would 
need to set the params also, which seems like more complex 
handling/overhead.
    Is there a solution workaround to this? I've searched, and of course 
Rick has written some good articles on this, but I haven't seen the 
situation where it needs to return to a dynamic page *with* the params, 
aka the calling page.

    I suppose I could get the referer URI, but I don't think I could 
call mapping.getInput() that way.....

Thanks,

- Nic.

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


Re: Using validate() and returning to original page dynamically

Posted by "Michael J." <jm...@gmail.com>.
On 4/19/05, Joe Germuska <Jo...@germuska.com> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Using validate() and returning to original page dynamically

Posted by Joe Germuska <Jo...@Germuska.com>.
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

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

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


Re: Using validate() and returning to original page dynamically

Posted by "Michael J." <jm...@gmail.com>.
Hi Nic,

Struts does not allow to do things like this declaratively, but you
can do it manually.

1) you can obtain mapping name from the ActionMapping. This will
solidify the mapping name where you want to return, in code. I am not
sure that this is worse that to solidify it in struts-config.xml, but
people say that struts-config.xml is "more flexible in deploy time".
Hardly.

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.

Referer is a great thing, but some browsers and firewalls can turn it
off, so it is not reliable.

On the other hand, are you sure that you want to design your actions
like this? How about this:

* createItem - creates new empty item; forwards (better yet,
redirects) to editItem.
* editItem - allows to set/change value for a new or for an existing item;
* storeItem - persists modified item (inserts the one just created or
updates already existing, called from editItem);

But in this case, if you use redirection, you would need to patch the
path to the action too, to include item ID. If interested, see more
here: http://www.theserverside.com/articles/article.tss?l=RedirectAfterPost2.
It has a link to the source code, too.

WBR,
   Michael Jouravlev.

On 4/18/05, Nic Werner <we...@sonoma.edu> wrote:
> Hi again,
>     I'm calling the validate() method in my MappingDispatchAction to
> check errors on page. Which is fine, except that I want to return to the
> URL (Action w/params) that was called. I have two Actions, Edit and Add,
> which both refer to Attributes.jsp, and depending on the type param set,
> this page forwards to either Update or Insert, respectively. Therefore,
> setting 'input='Attributes.jsp' won't work as the bean isn't setup
> properly for the Edit Action.
>     So, the flow is that I want '/Edit.do?Id=32' to check for errors,
> and if there is one, return to '/Edit.do?Id=32', not Attributes.jsp. It
> isn't enough to set the type (Edit/Add) in the session scope, I would
> need to set the params also, which seems like more complex
> handling/overhead.
>     Is there a solution workaround to this? I've searched, and of course
> Rick has written some good articles on this, but I haven't seen the
> situation where it needs to return to a dynamic page *with* the params,
> aka the calling page.
> 
>     I suppose I could get the referer URI, but I don't think I could
> call mapping.getInput() that way.....
> 
> Thanks,
> 
> - Nic.

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