You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Glanville, Jay" <Ja...@NaturalConvergence.com> on 2004/03/16 01:46:51 UTC

What is the best way to pass a parameter to a forward?

I'm trying to solve a problem, but I'm not sure my solution is the best
way.  Basically, I want to set a parameter on a forward within the
action's execute.

I'm in my action's execute method.  I've just successfully performed
some work, and I now want to forward/redirect to the next page.  So,
I've got some code that looks like this:

   public ActionForward execute(ActionMapping.....) {
      // do some work
      ActionForward goto = mapping.findForward( "success" );
      return goto;
   }

With an action mapping that looks like this:

   <action
      path="/EntrySave"
      type="com.package.EntrySaveAction"
      name="EntryForm"
      validate="true"
      input="/Entry.do">
      <forward name="success" redirect="true" path="/Container.do"/>
      <forward name="failure" redirect="false" path="/Entry.do" />
   </action>

Basically, if I left the EntrySaveAction.execute do what's doing right
now, then upon successful completion, it would redirect to
"/Container.do".  However, what I want it to do is redirect to
"/Container.do?id=45", where 45 is the id of the container that I want
to go to.  The value of "45" is dynamic, and is know at the time of the
EntrySaveAction.execute command.

The way I'm currently doing it is something like this:

   public ActionForward execute(ActionMapping.....) {
      // do some work
      ActionForward goto = mapping.findForward( "success" );
      String path = goto.getPath();
      path += "?id=" + container.getId();
      goto.setPath( path );
      return goto;
   }

Is this the correct way to do this?  Is there a better way?

Thanks in advance

JDG


--
Jay Glanville

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


Re: What is the best way to pass a parameter to a forward?

Posted by Joshua Tuberville <jo...@eharmony.com>.
Jay,

Since Struts does not differentiate between a POST and a GET, passing
data as query parameters is the same as form data.  You could create a
form that has only the one field "id" which would be populated if you
called a URL of "/someAction?id=foo".  This is what I use when I create
dynamic links.  This is when I want this id to come from a link on a
page.

However when I am chaining Actions and need to pass values I usually set
values like that in the request scope using 

request.setAttribute(SOME_CONSTANT, idObject);

then just forward to the new action

Hope this helps,

Joshua

On Mon, 2004-03-15 at 16:46, Glanville, Jay wrote:
> I'm trying to solve a problem, but I'm not sure my solution is the best
> way.  Basically, I want to set a parameter on a forward within the
> action's execute.
> 
> I'm in my action's execute method.  I've just successfully performed
> some work, and I now want to forward/redirect to the next page.  So,
> I've got some code that looks like this:
> 
>    public ActionForward execute(ActionMapping.....) {
>       // do some work
>       ActionForward goto = mapping.findForward( "success" );
>       return goto;
>    }
> 
> With an action mapping that looks like this:
> 
>    <action
>       path="/EntrySave"
>       type="com.package.EntrySaveAction"
>       name="EntryForm"
>       validate="true"
>       input="/Entry.do">
>       <forward name="success" redirect="true" path="/Container.do"/>
>       <forward name="failure" redirect="false" path="/Entry.do" />
>    </action>
> 
> Basically, if I left the EntrySaveAction.execute do what's doing right
> now, then upon successful completion, it would redirect to
> "/Container.do".  However, what I want it to do is redirect to
> "/Container.do?id=45", where 45 is the id of the container that I want
> to go to.  The value of "45" is dynamic, and is know at the time of the
> EntrySaveAction.execute command.
> 
> The way I'm currently doing it is something like this:
> 
>    public ActionForward execute(ActionMapping.....) {
>       // do some work
>       ActionForward goto = mapping.findForward( "success" );
>       String path = goto.getPath();
>       path += "?id=" + container.getId();
>       goto.setPath( path );
>       return goto;
>    }
> 
> Is this the correct way to do this?  Is there a better way?
> 
> Thanks in advance
> 
> JDG
> 
> 
> --
> Jay Glanville
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 

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


Re: What is the best way to pass a parameter to a forward?

Posted by Martin Cooper <ma...@apache.org>.
What you are doing will not work - at least, not the way you are doing it.
You are trying to modify the ActionForward instance that is owned by Struts,
and calling setPath() on that instance will result in an
IllegalStateException.

You need to create your own ActionForward instance, instead of trying to
modify Struts' one. You can do that with something like this:

    ActionForward goto = mapping.findForward( "success" );
    String path = ...; // Put together your new path
    ActionForward myGoto = new ActionForward(path, goto.getRedirect());
    return myGoto;

--
Martin Cooper


"Glanville, Jay" <Ja...@NaturalConvergence.com> wrote in message
news:827BF123F7B64D48A7F305D668884075815881@ex2k.bitheadsinc.local...
I'm trying to solve a problem, but I'm not sure my solution is the best
way.  Basically, I want to set a parameter on a forward within the
action's execute.

I'm in my action's execute method.  I've just successfully performed
some work, and I now want to forward/redirect to the next page.  So,
I've got some code that looks like this:

   public ActionForward execute(ActionMapping.....) {
      // do some work
      ActionForward goto = mapping.findForward( "success" );
      return goto;
   }

With an action mapping that looks like this:

   <action
      path="/EntrySave"
      type="com.package.EntrySaveAction"
      name="EntryForm"
      validate="true"
      input="/Entry.do">
      <forward name="success" redirect="true" path="/Container.do"/>
      <forward name="failure" redirect="false" path="/Entry.do" />
   </action>

Basically, if I left the EntrySaveAction.execute do what's doing right
now, then upon successful completion, it would redirect to
"/Container.do".  However, what I want it to do is redirect to
"/Container.do?id=45", where 45 is the id of the container that I want
to go to.  The value of "45" is dynamic, and is know at the time of the
EntrySaveAction.execute command.

The way I'm currently doing it is something like this:

   public ActionForward execute(ActionMapping.....) {
      // do some work
      ActionForward goto = mapping.findForward( "success" );
      String path = goto.getPath();
      path += "?id=" + container.getId();
      goto.setPath( path );
      return goto;
   }

Is this the correct way to do this?  Is there a better way?

Thanks in advance

JDG


--
Jay Glanville




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