Spring Web Flow请求映射

时间:2012-07-02 14:26:28

标签: spring spring-mvc mapping spring-webflow-2

我将Spring Web Flow 2添加到一个非常大的现有Web应用程序中,该应用程序当前不使用Spring MVC或Web Flow。我的任务是通过访问mySite.com/flows来触发Web Flow,我遇到了困难。我的方法是设置DispatcherServlet,其映射为/flows/*,并将Web Flow映射到/flows。这是我的web.xml,其中配置了DispatcherServlet:

<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>

我尝试了几种方法让Web Flow映射到/flows。我的第一次尝试是使用带有base-path设置的流注册表:

<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>

我的理解是,这将采用/WEB-INF/app/flows内的文件夹结构来创建请求映射。我的第一个测试是在名为booking-flow.xmlbooking)的子文件夹中添加我的流/WEB-INF/app/flows/booking。而且,太棒了! - 它按预期工作。我能够从mySite.com/flows/booking访问该流程。好的,但我不希望在网址中显示/booking,因此我将booking-flow.xml移出booking文件夹,然后直接转到WEB-INF/app/flows,并期望为我工作,但事实并非如此 - 我根本不认为流程是映射的。

有谁知道如何将流映射到DispatcherServlet映射的根目录,还是有更好的方法来解决这个问题?我不希望DispatcherServlet处理我的应用程序中/flows之外的任何请求。它只是我,还是Spring Web Flow上的文档很少?

谢谢!

3 个答案:

答案 0 :(得分:4)

您可以使用SimpleUrlHandlerMapping这种方式将流程映射到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>

答案 1 :(得分:1)

另一种方式可能是这样的

<!-- Registry of executable flows -->
<flow:flow-registry flow-builder-services="flowBuilderServices"
    id="flowRegistry" base-path="/WEB-INF/flows/">
    <flow:flow-location id="hello" path="hello-flow.xml" />
    <flow:flow-location path="start-flow.xml" id="main" />

</flow:flow-registry>

使用id强制调用流程,如 hello .htm或 hello .html,具体取决于url映射。将加载相应文件中的第一个视图状态。

答案 2 :(得分:0)

根据您当前的文件结构设置,您可以执行

<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>

即你明确地给出了id,在这种情况下,适用的URL是 - mySite.com/flows

OR

您可以从基本上下文中删除流 -

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

并在/WEB-INF/app/flows/booking-flow.xml中输出您的文件。这将自动生成与基本路径和文件名之间的路径相对应的ID - 这将是flows,并且适用的网址将是mySite.com/flows