You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Felix Khazin <fk...@ravewireless.com> on 2006/07/20 21:46:45 UTC

Struts forward and redirect

Hi all,

 

I have a quick question:

 

My struts-config.xml has a forward line such as:

 

<forward name="show" path="/application.do"/>

 

In my controller if I set an attribute such as:

 

Request.setAttribute("test", "test string");

Return (mapping.findForward("show"));

 

This works fine and I am able to use test in the view.

 

But if I change the forward to "redirect=true" this functionality
disappears and I am no longer able to use the attributes set in the
controller.

 

Does anyone know why I can't do this with a redirect, and how I may be
able to still use a redirect and get the attributes in the view?

 

Thank you very much!

 

Felix


Re: Struts forward and redirect

Posted by Jakub Milkiewicz <jm...@gmail.com>.
Hi
If it is not still clear for you read:
http://organicveggie.wordpress.com/2006/01/24/struts-redirect-wparameters/

2006/7/20, Jakub Milkiewicz <jm...@gmail.com>:
>
> Hi
> It is quite easy thing...
> If you forward, request object is passed to "new resource" and if you
> redirect, server sends answer to browser to make a new request to new
> resource.In a latter, your request object on which you set some attributes
> is gone - "new resource" sees new request.
> I hope that's clear.
> If you wanna to redirect and also add some parameters to request you need
> to do it programmatically: in your action you can retrieve path to resource
> you wanna to redirect (use mapping.findForward ("dfdd") ).   Next, you can
> get "path" to redirect but it is need to be a little tuned to add your
> parameters (some kind of string manipulation to add http parameters).
> Important thing: you can't modify ActionForward object you get by using
> mapping.findForward() cause you will get configuration is frozen
> exception. You need to create new ActionForward and copy properties you need
> from actionForward you got by mapping.findForward() instruction to new
> created object. In your case i think it will be path property (after copying
> path, tune it now adding some http params) and redirect property.
> Then in your action return this new actionforward object.
> Please see some pseude code:
> Action{
> execute(){
> ActionForward oldOne = mapping.findForward("bla");
> ActionForward newOne = new ActionForward();
> String oldOnePath = oldOne.getPath();
> oldOnePath = oldOnePath + "?foo=bar";
> newOne.setPath(oldOnePath);
> newOne.setRedirce(oldOne.getRedirect());
> return newOne;
> }
>
> }
>
>
> 2006/7/20, Felix Khazin <fk...@ravewireless.com>:
>
> > Hi all,
> >
> >
> >
> > I have a quick question:
> >
> >
> >
> > My struts-config.xml has a forward line such as:
> >
> >
> >
> > <forward name="show" path="/application.do"/>
> >
> >
> >
> > In my controller if I set an attribute such as:
> >
> >
> >
> > Request.setAttribute("test", "test string");
> >
> > Return (mapping.findForward("show"));
> >
> >
> >
> > This works fine and I am able to use test in the view.
> >
> >
> >
> > But if I change the forward to "redirect=true" this functionality
> > disappears and I am no longer able to use the attributes set in the
> > controller.
> >
> >
> >
> > Does anyone know why I can't do this with a redirect, and how I may be
> > able to still use a redirect and get the attributes in the view?
> >
> >
> >
> > Thank you very much!
> >
> >
> >
> > Felix
> >
> >
> >
>

Re: Struts forward and redirect

Posted by Jakub Milkiewicz <jm...@gmail.com>.
Hi
It is quite easy thing...
If you forward, request object is passed to "new resource" and if you
redirect, server sends answer to browser to make a new request to new
resource.In a latter, your request object on which you set some attributes
is gone - "new resource" sees new request.
I hope that's clear.
If you wanna to redirect and also add some parameters to request you need to
do it programmatically: in your action you can retrieve path to resource you
wanna to redirect (use mapping.findForward("dfdd") ).   Next, you can get
"path" to redirect but it is need to be a little tuned to add your
parameters (some kind of string manipulation to add http parameters).
Important thing: you can't modify ActionForward object you get by using
mapping.findForward() cause you will get configuration is frozen exception.
You need to create new ActionForward and copy properties you need from
actionForward you got by mapping.findForward() instruction to new created
object. In your case i think it will be path property (after copying path,
tune it now adding some http params) and redirect property.
Then in your action return this new actionforward object.
Please see some pseude code:
Action{
execute(){
ActionForward oldOne = mapping.findForward("bla");
ActionForward newOne = new ActionForward();
String oldOnePath = oldOne.getPath();
oldOnePath = oldOnePath + "?foo=bar";
newOne.setPath(oldOnePath);
newOne.setRedirce(oldOne.getRedirect());
return newOne;
}

}


2006/7/20, Felix Khazin <fk...@ravewireless.com>:
>
> Hi all,
>
>
>
> I have a quick question:
>
>
>
> My struts-config.xml has a forward line such as:
>
>
>
> <forward name="show" path="/application.do"/>
>
>
>
> In my controller if I set an attribute such as:
>
>
>
> Request.setAttribute("test", "test string");
>
> Return (mapping.findForward("show"));
>
>
>
> This works fine and I am able to use test in the view.
>
>
>
> But if I change the forward to "redirect=true" this functionality
> disappears and I am no longer able to use the attributes set in the
> controller.
>
>
>
> Does anyone know why I can't do this with a redirect, and how I may be
> able to still use a redirect and get the attributes in the view?
>
>
>
> Thank you very much!
>
>
>
> Felix
>
>
>