You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pierre Thibaudeau <pi...@gmail.com> on 2007/02/24 07:29:24 UTC

[s1] URL with parameters: how to redirect?

[using Struts 1.3.5]

I have a fairly complex search form which (once its content is checked
as valid and unambiguous) should *redirect* to a results action that
outputs a results.jsp page.

The primary reason for *redirecting* is that I want the browser's URL
bar to display an address which entirely determines the results page.
(So that we can link to the results page, without going through the
form). For example, the URL might look like:

www.mysite.com/results.do?param1=a647d&param2=3g442b

So far, the two actions are as follows:

    <action
      path="/search"
      name="searchForm"
      scope="request"
      type="com.mysite.searchAction"
      validate="true"
      input=".search">
      <forward name="results" path="/results.do" redirect="true" />
      <forward name="noresult" path=".noresult" redirect="false" />
    </action>

    <action
      path="/results"
      scope="request"
      type="com.mysite.resultsAction"
      validate="false">
      <forward name="success" path=".results" redirect="false" />
    </action>

(The paths ".noresult" and ".results" are tiles.)

Inside my searchAction, in order to append the desired parameters, I
construct the forward more or less like this:

String resultsForward = mapping.findForward("results").getPath();
resultsForward += "?" + "criterionId=" + criterionId + "&" + "start="
+ startYear;
return new ActionForward(resultsForward);

The problem with this piece of code is that I am not using the local
redirect as defined in struts-config.html, and Struts doesn't realize
that my home-engineered new ActionForward is meant to be a *redirect*
and not a forward.  When the results page appears, the URL bar still
shows:  "http://www.mysite.com/search.do".

I would like either to:
1) instruct Struts that my newly formed forward is in fact a redirect.
2) or better, I would like to use my predefined "results" forward but
attach to it a map of parameters (in the same way that you can attach
a map-bean of parameters to an <html:link> tag).

Are options 1 or 2 feasible?

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


Re: [s1] URL with parameters: how to redirect?

Posted by Pierre Thibaudeau <pi...@gmail.com>.
> Or you can use ActionRedirect
> http://struts.apache.org/1.x/apidocs/org/apache/struts/action/ActionRedirect.html
> which sets this flag automatically.

Thank you!  That's a significant improvement!

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


Re: [s1] URL with parameters: how to redirect?

Posted by Michael Jouravlev <jm...@gmail.com>.
On 2/23/07, Pierre Thibaudeau <pi...@gmail.com> wrote:
> [using Struts 1.3.5]
>
> I have a fairly complex search form which (once its content is checked
> as valid and unambiguous) should *redirect* to a results action that
> outputs a results.jsp page.
>
> The primary reason for *redirecting* is that I want the browser's URL
> bar to display an address which entirely determines the results page.
> (So that we can link to the results page, without going through the
> form). For example, the URL might look like:
>
> www.mysite.com/results.do?param1=a647d&param2=3g442b
>
> So far, the two actions are as follows:
>
>     <action
>       path="/search"
>       name="searchForm"
>       scope="request"
>       type="com.mysite.searchAction"
>       validate="true"
>       input=".search">
>       <forward name="results" path="/results.do" redirect="true" />
>       <forward name="noresult" path=".noresult" redirect="false" />
>     </action>
>
>     <action
>       path="/results"
>       scope="request"
>       type="com.mysite.resultsAction"
>       validate="false">
>       <forward name="success" path=".results" redirect="false" />
>     </action>
>
> (The paths ".noresult" and ".results" are tiles.)
>
> Inside my searchAction, in order to append the desired parameters, I
> construct the forward more or less like this:
>
> String resultsForward = mapping.findForward("results").getPath();
> resultsForward += "?" + "criterionId=" + criterionId + "&" + "start="
> + startYear;
> return new ActionForward(resultsForward);
>
> The problem with this piece of code is that I am not using the local
> redirect as defined in struts-config.html, and Struts doesn't realize
> that my home-engineered new ActionForward is meant to be a *redirect*
> and not a forward.  When the results page appears, the URL bar still
> shows:  "http://www.mysite.com/search.do".
>
> I would like either to:
> 1) instruct Struts that my newly formed forward is in fact a redirect.
> 2) or better, I would like to use my predefined "results" forward but
> attach to it a map of parameters (in the same way that you can attach
> a map-bean of parameters to an <html:link> tag).
>
> Are options 1 or 2 feasible?

ActionForward has several constructors:
http://struts.apache.org/1.x/apidocs/org/apache/struts/action/ActionForward.html
You need one with redirect parameter, set it to true.

Or you can use ActionRedirect
http://struts.apache.org/1.x/apidocs/org/apache/struts/action/ActionRedirect.html
which sets this flag automatically.

Michael.

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


Re: [s1] URL with parameters: how to redirect?

Posted by Pierre Thibaudeau <pi...@gmail.com>.
> I would like either to:
> 1) instruct Struts that my newly formed forward is in fact a redirect.
> 2) or better, I would like to use my predefined "results" forward but
> attach to it a map of parameters (in the same way that you can attach
> a map-bean of parameters to an <html:link> tag).
>
> Are options 1 or 2 feasible?

I asked my question too quickly, and I just found out that 1) was very
feasible, using ActionForward's "setRedirect(boolean)" method.  That
answers option 1).

Still I think that option 2) would be neater.  Is option 2) an option?  ;)

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