When I search the Web forum for questions on chaining controllers (not discussed in Professional Java Development With the Spring Framework), Spring developers reply that 'for advanced navigation needs, you need to look at Spring Web Flow'. Huh?
Corby,
Just to address this one point quickly.
If you look at the Spring MVC Controller interface, youŽll see Controller chaining really doesnt make sense. A controller is expected to broker a request and return a logical pointer to a view provisioned with all renderable model data. There is no notion of a ModelAndView that gets passed around as part of a chain.
So the answer here, though vague, was a good general recommendation. The Spring MVC Controller interface is simply not designed for this.
Spring Web Flow, on the other hand, is integrated with Spring MVC (see FlowController in the webflow.mvc package) and has a much more elaborate action model invokable from within an architecture based on a finite state machine, with the capability to chain command logic. So certainly, one of the reasons SWF exists is to serve this chaining (CoR) need, and we consider use of a FlowController to delegate to a webflow to achieve this a good solution. You could also have a MVC Controller delegate to a more generic CoR framework, like commons-chain, but you will find SWF more capable.
So to give you an example of what SWF can do, take the following code snippet:
Code:
public class MySimpleFlowBuilder extends AbstractFlowBuilder {
public String flowId() {
return "simpleFlow";
}
public void buildStates() {
addEndState("doSomeStuffThenDisplayAPage", "thePageName").setEntryAction(
new CompositeAction(new Action[] {
method("method1", action("myFlowAction")),
method("method2", action("myFlowAction")),
method("method3š, action("myFlowAction") }));
}
}
A very simple flow definition, that when invoked (by hitting the url /flowcontroller.htm?_flowId=simpleFlow) will enter the "doSomeStuffThenDisplayAPage" state, execute those action methods in an ordered chain ("method1", "method2", "method3"), and request the rendering of "aPage" which is resolved to a renderable template using a standard Spring MVC ViewResolver.
You gain additional benefits here in that your action logic is now decoupled from the underlying HTTP API, so it can be run in a Portlet environment, for example. In addition, with the upcoming WebFlow RC1, you can invoke command logic encapsulated directly within standard POJOs with no dependencies on webflow at all. In this case, youŽre typically telling webflow to invoke your business logic directly, and webflow handles the necessary controller glue.
So while this is a very trivial flow that lives only for a single request (is essentially stateless) & does nothing more than execute some logic in a chain and display a results page, it shows some of what webflow is capable of, invokable directly from Spring MVC (or Struts for that matter).
An aside: if you dont like looking up your actions by name like in the flow definition above, and instead prefer lookup by type or by direct reference, you can do that as well.
Keith