You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ney André de Mello Zunino <zu...@inf.ufsc.br> on 2007/01/09 13:08:55 UTC

ActionForwards from the config file not to be tampered?

Hello.

While trying to find a way to pass parameters from an action to another 
(which I actually ended up doing by appending the query string to the 
ActionForward's path), I noticed that I could not work directly on the 
action from the config file (the one returned from 
mapping.findForward()). Attempts to alter that action would result in 
failure, e.g. fwdAction.setPath(fwdAction.getPath() + ...)

What I had to do was create a new ActionForward and set its attributes:

ActionForward newAction = new ActionForward();
newAction.setPath(fwdAction.getPath() + ...);
newAction.setRedirect(true);
return newAction;

Now what I am trying to understand is why this is the case. Is is 
because ActionForwards defined in the config file are not supposed to be 
modified? I am just beginning with Struts, so please bear with me.

Thank you,

-- 
Ney André de Mello Zunino


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


Re: ActionForwards from the config file not to be tampered?

Posted by Ney André de Mello Zunino <zu...@inf.ufsc.br>.
Christopher Schultz escreveu:

> Struts comes with something like this already
> (org.apache.struts.action.ActionRedirect).

Yeah, I did find some references to that class and was looking forward 
to using it. Unfortunately, the company I have just joined (and its 
home-built framework) relies on an outdated version of Struts.

> The reason that you shouldn't be modifying these objects is that they
> are intended to be created once and used many times. Struts loads its
> configuration into a series of objects in memory and doesn't want you to
> modify that configuration during runtime. If you were to change the
> parameters for a particular ActionForward, then they would be changed
> for every use of that ActionForward thereafter. It would be a mess.
> 
> So, the developers chose a solution that would be conservative on memory
> (only one object ever created for an ActionForward in the config file)
> and processor time (don't have to waste time creating and initializing
> those objects). In order to protect from accidental changes, these
> objects are "locked" after they are configured to make sure that folks
> like you don't stumble into a situation where your Struts app starts
> doing seriously unexpected things.

Thank you very much for your detailed explanation and the code. I had 
already got things to work using that approach. It's just that I dislike 
not understanding the whys of things.

Best regards,

-- 
Ney André de Mello Zunino


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


Re: ActionForwards from the config file not to be tampered?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ney,

Ney André de Mello Zunino wrote:
> What I had to do was create a new ActionForward and set its attributes:
> 
> ActionForward newAction = new ActionForward();
> newAction.setPath(fwdAction.getPath() + ...);
> newAction.setRedirect(true);
> return newAction;

I do the same thing, but I've written a class to help wrap all that
logic so it's easier to do so. The class even wraps an existing
ActionForward and extends it, so it acts just like a regular
ActionForward except that you can add parameters and even an anchor to
it. You can find the code for it as an attachment to this Struts
enhancement request: http://issues.apache.org/bugzilla/show_bug.cgi?id=36037

Struts comes with something like this already
(org.apache.struts.action.ActionRedirect).

> Now what I am trying to understand is why this is the case. Is is
> because ActionForwards defined in the config file are not supposed to be
> modified? I am just beginning with Struts, so please bear with me.

The reason that you shouldn't be modifying these objects is that they
are intended to be created once and used many times. Struts loads its
configuration into a series of objects in memory and doesn't want you to
modify that configuration during runtime. If you were to change the
parameters for a particular ActionForward, then they would be changed
for every use of that ActionForward thereafter. It would be a mess.

So, the developers chose a solution that would be conservative on memory
(only one object ever created for an ActionForward in the config file)
and processor time (don't have to waste time creating and initializing
those objects). In order to protect from accidental changes, these
objects are "locked" after they are configured to make sure that folks
like you don't stumble into a situation where your Struts app starts
doing seriously unexpected things.

Look into using ActionRedirect. Or, if you want a touch more
flexibility, use my class found in the bug mentioned above.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFo8Up9CaO5/Lv0PARAnM0AJwNogfxrU2Ly/M5zu3DZwe5q2fD0ACffy4N
SQryCdnDNf+bBBf2CY3IjeQ=
=b7NR
-----END PGP SIGNATURE-----

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