This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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);
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 ]
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.
Joined: Feb 15, 2005
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.
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.