I've been through Spring forums for quite a while now trying to figure out how to SIMPLY forward a request to a controller from another controller
(keywords: forward, forwarding, controller chaining, …). It seems that this basic function has no easy implementation.
The problem started when I wanted to forward the request to a controller that is dynamically determined (hence the BeanFactory wouldn't help).
First: I simply tried to call the required controller as a view (
return new ModelAndView(viewName)) and yes, I agree it is not a view. My intelligent
FreemarkerViewResolver(perhaps too intelligent) kept appending the “.ftl” to that imaginary view.
Second: I tried (as recommended by some) to upgrade to Spring 1.1.3 where a forward can be used (similarly as “redirect:”) using a
UrlBasedViewResolver- from release notes
(UrlBasedViewResolver supports a “forward:” prefix too, for forwards to other controllers through special view names
I used a
UrlBasedViewResolverchained to the
FreeMarkerViewResolverthat I have (chaining multiple view resolvers through their “order” property).
That didn't work either! using the
<property name="order"><value>1</value></property>gave me some weird exception that the view resolvers didn't have a property setter for property “order” !!!). (didn't have time to look at this).
Third: I used a
RedirectViewand placed those attributes I wanted to place on the request as session ones (not very sexy approach, but, it worked anyway).
Fourth: I went back to the basics, and used the
getRequestDispatcher()to get the request dispatcher and called the wonderful
forward()on it (returning a null from my handleRequest). This also worked, but it seems like a departure from Spring recommendations of NOT returning a null value.
Sixth: I thought of returning a
secondController.handleRequest(...)from within my firstController's handleRequest(..) but this wouldn't have helped either, as the secondController is runtime determined.
As I became more and more aware that this wasn't a straight method call; a reply from kdonald (Spring Team) was posted on one of the discussions
“forwarding to a controller without RedirectView“. It seems that spring indeed doesn't have this “forward” action, and that Spring's web flow will solve this issue and provide enhanced chaining functions (expected release 1.2). Can't wait to try it.