Struts - Request attributes while forwarding the request

Y

Yogesh

Hello all

I have an action, say action 1, which forwards the request to action2.
The problem is that I have set a few attributes in the request in
action1, that I need to use in action2. Due to some environment
restrictions, I cannot set those attributes in the session. This is my
first Struts application, and I think that whenever we use a forward,
Struts creates a new request, and all the attributes are lost. Any
ideas how to work around it?

Thanks
-Yogesh
 
R

Ryan Stewart

Yogesh said:
Hello all

I have an action, say action 1, which forwards the request to action2.
The problem is that I have set a few attributes in the request in
action1, that I need to use in action2. Due to some environment
restrictions, I cannot set those attributes in the session. This is my
first Struts application, and I think that whenever we use a forward,
Struts creates a new request, and all the attributes are lost. Any
ideas how to work around it?
Struts does no such thing. Some containers make a questionable
interpretation of the specification and do some odd things. This is why
action chaining, which is what you are describing, is highly discouraged.
It's in the Struts documentation.
 
W

Wendy S

Yogesh said:
I have an action, say action 1, which forwards the request to action2.
The problem is that I have set a few attributes in the request in
action1, that I need to use in action2. Due to some environment
restrictions, I cannot set those attributes in the session. This is my
first Struts application, and I think that whenever we use a forward,
Struts creates a new request, and all the attributes are lost. Any
ideas how to work around it?

What are you doing that you need two Actions in a row? This is known as
action chaining and is generally frowned upon. Occasionally it turns out to
be the right solution, but more often than not, there's a better way to
design the webapp.

A forward should preserve the request. Here's a thread in which someone has
the opposite problem-- he's forwarding and does NOT want the values in the
request to be there:
http://www.mail-archive.com/[email protected]/msg00530.html

Are you possibly writing request.getAttribute(...) when you mean
request.getParameter(...)? You say 'attribute' above, but when people
complain that things are missing from the request, they're usually talking
about parameters-- the values that the user has entered in a form and
submitted.
 
D

Daniel Rohe

As described in previous posts you are doing action chaining. When you
forward from action 1 to action 2 as far as I understood the Struts
documentation the request parameters are preserved. If you want to remove
the request parameters you can do a redirect. The forward definition in the
Struts configuration file (struts-config.xml) has a attribute
redirect="true|false" where you can set whether the forward is a redirect or
a normal forward. Hope that helps.

Daniel
 
Y

Yogesh

Hi Wendy, thanks for your reply. You are right, it's a bad design, but
that is something I have to live with for the time being. I am using
request.setAttribute() and request.getAttribute(). And for some
reasons, my attributes are getting lost. Any ideas?

Thanks
-Yogesh
 
Y

Yogesh

Hi Wendy, thanks for your reply. You are right, it's a bad design, but
that is something I have to live with for the time being. I am using
request.setAttribute() and request.getAttribute(). And for some
reasons, my attributes are getting lost. Any ideas?

Thanks
-Yogesh
 
Y

Yogesh

Hi Wendy, thanks for your reply. You are right, it's a bad design, but
that is something I have to live with for the time being. I am using
request.setAttribute() and request.getAttribute(). And for some
reasons, my attributes are getting lost. Any ideas?

Thanks
-Yogesh
 
Y

Yogesh

Thanks for your replies ppl....one more thing that you guys might be
interested in knowing. ActionForward in my struts-config.xml looks like
this:

<forward name="success" path="/repoptions.htm" redirect="true"/>
Is redirect="true" causing a problem?

Thanks
-Yogesh
 
Y

Yogesh

Thanks for your replies ppl....one more thing that you guys might be
interested in knowing. ActionForward in my struts-config.xml looks like
this:

<forward name="success" path="/repoptions.htm" redirect="true"/>
Is redirect="true" causing a problem?

Thanks
-Yogesh
 
Y

Yogesh

Thanks for your replies ppl....one more thing that you guys might be
interested in knowing. ActionForward in my struts-config.xml looks like
this:

<forward name="success" path="/repoptions.htm" redirect="true"/>
Is redirect="true" causing a problem?

Thanks
-Yogesh
 
W

Wendy S

Yogesh said:
Thanks for your replies ppl....one more thing that you guys might be
interested in knowing. ActionForward in my struts-config.xml looks like
this:
<forward name="success" path="/repoptions.htm" redirect="true"/>
Is redirect="true" causing a problem?

You're not doing a forward. You're redirecting. Redirecting causes the
browser to make a new request, and that's why the things you put in the
request aren't there anymore.

Note that Struts is not constructing the new request, it is telling the
browser to do so.
 
Y

Yogesh

Thanks guys, brilliant answers. I changed redirect='false' and it
worked. I know it's a bad design, but I'm debugging an already written
application.

Thanks
-Yogesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top