You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Wendy Smoak <We...@asu.edu> on 2003/07/23 02:08:15 UTC

ActionForward question & encoding URL's

The only way I've ever used an ActionForward is by looking it up from the
ActionMapping... mapping.findForward( "someString" );

Today I really needed to set some request parameters, and of course there is
no request.setParameter() method.  I know there's some way to use a request
wrapper, but that didn't seem right.  So I pieced the URL together and
created a new ActionForward.

I have a form into which the user types a name.  The webapp then displays a
"resolution screen" of possible matches so they can pick one, then they go
back where they came from with that choice selected.  (Done by an awful
arrangement of setting request attributes, and the resolution form isn't
even a Struts form since I don't know until request time what the <form
action="..."> should be.  And it doesn't post to itself.)

Except if there is only one match, I don't want to show the resolution
screen, I want to go right back to the original form.

So here's what I have if resolution only turns up one hit:

String path = "/" + request.getAttribute( "returnTo" ) 
             + "?selectedId=" + selectedId
             + "&userAction=" + request.getAttribute( "userAction" )
             + "&returningFromResolution=true";

return new ActionForward( response.encodeURL( path ) );

(While we're here, I looked in the Struts source code for a hint on encoding
URL's, and found response.encodeURL().  Older code had URLEncoder.encode(),
but I didn't find that class in a quick search.  Comments?)

So, does the code above send anyone into screaming fits of "Don't DO that!"?
I'm not entirely sure what Action Chaining _is_ but I think I might be doing
it.  I'm up for suggestions on the reusable resolution Action and Form, I'd
rather it be a Struts form, but I'm not sure how to do it.

Thanks in advance for the pearls of wisdom.

-- 
Wendy Smoak
Applications Systems Analyst, Sr.
Arizona State University, PA, IRM 

Re: ActionForward question & encoding URL's

Posted by Kris Schneider <kr...@dotech.com>.
Wendy,

Forget the call to encodeURL, you don't need it. The only time you need 
to rewrite URLs is when you're handing them to a client for use in a 
future request. In this case, all you're doing is transferring control 
within the server, the client is never involved. In the case of an 
ActionForward (ForwardConfig) that has its "redirect" property set to 
"true", Struts will take care of calling 
HttpServletResponse.encodeRedirectURL. The difference with a redirect is 
that you *are* handing a URL back to the client and asking it to issue a 
completely new request, hence the need for URL rewriting. Otherwise, I 
think what you've got is fine for dynamically creating a forward. The 
only thing that appears ugly is that your "returnTo" attribute will have 
to know whether you're using path (e.g. "/do/*) or extension (e.g. 
"*.do") mapping for the Struts servlet, right? Unless you squirrel away 
the original request URI somehow...

In terms of dynamically handling the "action" attribute of <html:form>, 
its value can be determined by a request-time expression, if it helps. 
Or you can use an EL expression with <html-el:form> (Struts-EL taglib).

Wendy Smoak wrote:
> The only way I've ever used an ActionForward is by looking it up from the
> ActionMapping... mapping.findForward( "someString" );
> 
> Today I really needed to set some request parameters, and of course there is
> no request.setParameter() method.  I know there's some way to use a request
> wrapper, but that didn't seem right.  So I pieced the URL together and
> created a new ActionForward.
> 
> I have a form into which the user types a name.  The webapp then displays a
> "resolution screen" of possible matches so they can pick one, then they go
> back where they came from with that choice selected.  (Done by an awful
> arrangement of setting request attributes, and the resolution form isn't
> even a Struts form since I don't know until request time what the <form
> action="..."> should be.  And it doesn't post to itself.)
> 
> Except if there is only one match, I don't want to show the resolution
> screen, I want to go right back to the original form.
> 
> So here's what I have if resolution only turns up one hit:
> 
> String path = "/" + request.getAttribute( "returnTo" ) 
>              + "?selectedId=" + selectedId
>              + "&userAction=" + request.getAttribute( "userAction" )
>              + "&returningFromResolution=true";
> 
> return new ActionForward( response.encodeURL( path ) );
> 
> (While we're here, I looked in the Struts source code for a hint on encoding
> URL's, and found response.encodeURL().  Older code had URLEncoder.encode(),
> but I didn't find that class in a quick search.  Comments?)
> 
> So, does the code above send anyone into screaming fits of "Don't DO that!"?
> I'm not entirely sure what Action Chaining _is_ but I think I might be doing
> it.  I'm up for suggestions on the reusable resolution Action and Form, I'd
> rather it be a Struts form, but I'm not sure how to do it.
> 
> Thanks in advance for the pearls of wisdom.

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>



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