springspring-mvcmappingspring-webflow-2

Spring Web Flow request mappings


I am adding Spring Web Flow 2 to a very large existing web application that does not currently use Spring MVC or Web Flow. My task is to have the Web Flow triggered by going to mySite.com/flows, and I am having difficulties. My approach was to set up the DispatcherServlet with mapping of /flows/* and map Web Flow to /flows. Here is my web.xml where the DispatcherServlet is configured:

<servlet>
    <servlet-name>flow</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/flowContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>flow</servlet-name>
    <url-pattern>/flows/*</url-pattern>
</servlet-mapping>

I have tried several methods to get the Web Flow to map to /flows. My first try was to use a flow-registry with base-path setting:

<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF/app/flows">
    <webflow:flow-location-pattern value="**/*-flow.xml" />
</webflow:flow-registry>

My understanding is that this will take the folder structure inside of the /WEB-INF/app/flows to create the request mapping. My first test was to add my flow, booking-flow.xml inside a subfolder called booking (/WEB-INF/app/flows/booking). And, great! - it worked as expected. I was able to access the flow from mySite.com/flows/booking. OK, but I don't want /booking in the URL, so I moved the booking-flow.xml out of the booking folder, and straight into WEB-INF/app/flows and expected that to work for me, but it did not - I don't think the flow mapped at all.

Does anyone know how I can map a flow to the root of the DispatcherServlet mapping, or is there a better way to approach this? I don't want the DispatcherServlet to handle any requests outside of /flows in my application. Is it just me, or is there very little documentation available on Spring Web Flow?

Thanks!


Solution

  • You can use SimpleUrlHandlerMapping this way to map your flow to mySite.com/flows

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings" value="flows=flowController" />
    </bean>
    
    <bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>
    
    <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
    </webflow:flow-executor>
    
    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF/app/flows">    
        <webflow:flow-location path="booking-flow.xml" id="flows"/>
    </webflow:flow-registry>