This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instantiate ActionForward objects

 
Vani Bandargal
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("/testInvestigation.do?method=doView&testKey=" + testeKey);

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

Can some one confirm this?

Thanks in advance.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vani Bandargal
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
request.setAttribute("testKey",testKey);
return mapping.findForward(IConstants.SUCCESS_KEY); //this is actually "/testInvestigation.do?method=doView


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
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Merill for the explanation.
 
Brent Sterling
Ranch Hand
Posts: 948
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic