10.3. Dispatching to flows

The DispatcherServlet maps requests for application resources to handlers. A flow is one type of handler.

Registering the FlowHandlerAdapter

The first step to dispatching requests to flows is to enable flow handling within Spring MVC. To this, install the FlowHandlerAdapter:

<!-- Enables FlowHandler URL mapping -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor" />
</bean>
			

Defining flow mappings

Once flow handling is enabled, the next step is to map specific application resources to your flows. The simplest way to do this is to define a FlowHandlerMapping:

<!-- Maps request paths to flows in the flowRegistry;
     e.g. a path of /hotels/booking looks for a flow with id "hotels/booking" -->		
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="flowRegistry"/>
    <property name="order" value="0"/>
</bean>
			

Configuring this mapping allows the Dispatcher to map application resource paths to flows in a flow registry. For example, accessing the resource path /hotels/booking would result in a registry query for the flow with id hotels/booking. If a flow is found with that id, that flow will handle the request. If no flow is found, the next handler mapping in the Dispatcher's ordered chain will be queried or a "noHandlerFound" response will be returned.

Flow handling workflow

When a valid flow mapping is found, the FlowHandlerAdapter figures out whether to start a new execution of that flow or resume an existing execution based on information present the HTTP request. There are a number of defaults related to starting and resuming flow executions the adapter employs:

  • HTTP request parameters are made available in the input map of all starting flow executions.

  • When a flow execution ends without sending a final response, the default handler will attempt to start a new execution in the same request.

  • Unhandled exceptions are propagated to the Dispatcher unless the exception is a NoSuchFlowExecutionException. The default handler will attempt to recover from a NoSuchFlowExecutionException by starting over a new execution.

Consult the API documentation for FlowHandlerAdapter for more information. You may override these defaults by subclassing or by implementing your own FlowHandler, discussed in the next section.