Webflow Flow Location By URI?

人盡茶涼 提交于 2019-12-13 03:58:52

问题


I have a Spring MVC application that currently uses a SimpleUrlHandlerMapping to integrate several webflows. The bean definition looks something like this:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/process1/reservation.html">flowController</prop>

            <prop key="/process2/reservation.html">flowController</prop>
        </props>
    </property>
    <property name="order" value="0"/>
    <property name="alwaysUseFullPath" value="true" />
</bean>

What I need to do is decide which web flow to execute based on the URI. The rest of the definition looks like this:

<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
    <property name="flowExecutor" ref="flowExecutor"/>
</bean>

<flow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
    <flow:flow-execution-listeners>
        <flow:listener ref="flowExecutionListener" />
    </flow:flow-execution-listeners>    
</flow:flow-executor>

<flow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" >
    <flow:flow-location id="process1Reservation" path="/WEB-INF/flows/path/to/process1/reservationFlow.xml" />
    <flow:flow-location id="process2Reservation" path="/WEB-INF/flows/path/to/process2/reservationFlow.xml" />
</flow:flow-registry>

So, when I use the following URL, I expect to execute the first flow:

http://me.com/application/process1/reservation

(And similar with the second flow).

Am I overcompicating this? I do NOT want to create definitions with id's like "process1Reservation" if I can avoid doing so.

Thanks,

Jason


回答1:


First off, you can add wild card pattern to indicate (flow-location-pattern) that would create a rule as to how to register flows. This would enable you to register many flows with 1 rule instead of hard coding each one manually. (Note: below is the unofficial "standard and best practice" way to register flows)

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

So the above rule states:

  1. Start scanning in the /WEB-INF/flows directory (make sure it exists)
  2. For any sub directories look for any files that have the suffix -flow.xml
  3. For each match register this flow to the flow-registry

Once, you do this you can navigate to the registered flow by the url that it mapped to from anywhere in your application.

Using your example the flow:

/WEB-INF/flows/path/to/process1/reservation-flow.xml   // note: ends in "-flow.xml"

would be accessible via the http get request of:

http://(my-web-server-ip):(port)/path/to/process1

(Note: "flows" is NOT included in the url path because we set it as part of the base-path="/WEB-INF/flows" attribute in the flow-registry definition and note that the flow file name is not used at all in the request, only the directory the flow is defined in)

------------------- Now to ANSWER the Question --------------------------------

I would just create an MVC controller that redirects to the desired flow. Here is a crude example:

@RequestMapping(value = "/decideWhichFlow/{someUrlVal}", method = RequestMethod.GET) 
public String decideWhichFlow(@PathVariable String someUrlVal) {

    String flowName = null;

    // exec some logic to decide which flow to use based on "someUrlVal" passed via url
    if(someUrlVal.equalsIgnoreCase("1"))
        flowName = "process1";
    else 
        flowName = "process2";

    String redirectToFlow = "redirect:/path/to/" + flowName ;

    return redirectToFlow;  
}

Moreover, you could also create a master flow called "process" that can execute a "decision-state" that decides which sub-flows to call (subflow1,subflow2, etc...) based on some request Params but this might be overkill. I think the MVC controller solution is much simpler.

Also, the flow names "process1", "process2" will always be static. I don't see why would need to increment the names? If the increment has a special meaning in your application and is used in processing logic then you would pass it in as a request param in the flow and not make it a part of the flow name. If the flows represent different logic/process I would give them more meaningful names (e.g processReservation). No different than naming pojo class attributes. The name is only there for clarity as to the underlying function of the process.



来源:https://stackoverflow.com/questions/29411833/webflow-flow-location-by-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!