This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Struts and the fly likes instantiate ActionForward objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "instantiate ActionForward objects" Watch "instantiate ActionForward objects" New topic

instantiate ActionForward objects

Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
Can one do somethoing like this in action class method?
I was doing some code review and found this kind of codibg and my understanding is one should not do these kind of things in action class method. Page flow should go strictly go in struts config file.

return new ActionForward("/" + testeKey);

return new ActionForward("/jsp/test.jsp");

Can some one confirm this?

Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
As a general principle, I agree with you that ActionForwards should come from the struts-config.xml file and not be manufactured "on the fly". By following this principle, you have an accurate picture of the possible navigation paths for any action in the struts-config.xml file.

However, there are a few situations where instantiating an ActionForward can be useful.

One such situation is where you need to add parameters to the URL in the ActionForward. Even then it's still best to get the base URL from the config file and then add the parameter like this:

[ August 30, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
Thank you, Merril for your explanation with code example.
I got your point that we may need this if we need to pass parameters.

But I still have this question
Let say we need to add(pass) parameters to the url(Or to the corresponding action called)
We can always add these in request attributes, Correct?

Something like this:
return mapping.findForward(IConstants.SUCCESS_KEY); //this is actually "/

and then we can get the request attribute like this in other action class method which is being called.
String testKey=request.getAttribute("testKey");

My understanding is we can achieve the same using request attributes. If this is correct why(in what scenarios) we need to instantiate action forward?
Is there any difference in the two approaches that we discussed?

Thanks again for sharing your thoughts since I never thought of the approach that you explained.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It is true that in most cases you can pass information from one process to the next by setting and retrieving attributes in the HttpServletRequest object. However, sometimes an application is architected in such a way that a parameter is required.

For example, if you use the Struts DispatchAction, the name of the method to be invoked has to be passed in a parameter. It can't be passed as a request attribute.

Granted, the authors of the DispatchAction class could have written the class so that it reads a request attribute, but they didn't. Rather than write your own DispatchAction class, it's much easier to just "go with the flow" of the existing architecture and pass the parameter to the URL.
Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
Thank you, Merill for the explanation.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I admit that I have seen plenty of places in my project like your first example. I may have even written a few, though I try to avoid it. I would not forward directly to a jsp file.

It is pretty common that I have to append a parameter to a URL. Adding them to the request would not work for any forward that redirected. I don't fully understand why you cannot modify the fields on an ActionForward once it is "configured" but I guess Craig McClanahan had a reason. I have a little utility method that I use for this. It has the advantage over Merrill's example that it maintains the other forward settings from the struts-config file.

- Brent
jQuery in Action, 3rd edition
subject: instantiate ActionForward objects
It's not a secret anymore!