Spring Webflow - How to Get List of FLOW IDs

2020-02-15 03:23发布

问题:

What is the best way to get the full list of FLOW IDs generated by Spring Webflow?

Here is my configuration:

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

[UPDATE 1] I should clarify that I want to do this in Java code, not by inspecting my configuration.

[UPDATE 2] answer: requestContext.getActiveFlow().getApplicationContext()

回答1:

List of flow ids can be identified by the way they are defined in flow-registry. By default, flows will be assigned registry identifiers equal to their filenames minus the file extension, unless a registry base path is defined.

Let me explain this with examples:

Scenario 1: flow-location and base-path is not specified:

    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
        <webflow:flow-location path="/WEB-INF/pageFlows/example.xml" />  
    </webflow:flow-registry>

Flow id: example

Scenario 2: flow-location-pattern and base-path is not specified :

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

If you have flows like /WEB-INF/pageFlows/example1-flow.xml, /WEB-INF/pageFlows/example2-flow.xml, flow ids are: example1-flow, example2-flow respectively.

Scenario 3: Your own id is specified:

    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
        <webflow:flow-location path="/WEB-INF/pageFlows/example.xml" id="myExampleId" />  
    </webflow:flow-registry>

Flow id: myExampleId

Scenario 4: base-path is specified:

    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF">
        <webflow:flow-location path="/pageFlows/example.xml" />
    </webflow:flow-registry>

Flows will now be assigned registry identifiers equal to the the path segment between their base path and file name. Flow id: pageFlows

Scenario 5: flow-location-pattern and base-path is specified:

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

Flows will now be assigned registry identifiers equal to the the path segment between their base path and file name. So if you have flows located in /pageFlows1/example1, /pageFlows2/example2 directories within WEB-INF, flow ids are: pageFlows1, pageFlows2 respectively.

EDIT :

To get flow ids programmatically:

Assuming your flow controller and flowexecutor definitions as below in webflow-config xml file:

    <bean name="flowController" class="org.springframework.webflow.executor.mvc.FlowController">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>
    //flowRegistry is alredy mentioned in your question
    <flow:executor id="flowExecutor" registry-ref="flowRegistry">
        <flow:repository type="continuation" max-conversations="1" max-continuations="30" />
    </flow:executor>

You can retrieve flow definition ids registered as below: (I am calling this from a Controller which extends AbstractController, thats why you see getServletContext() method)

    ApplicationContext context = 
        (ApplicationContext)getServletContext().getAttribute(
            DispatcherServlet.SERVLET_CONTEXT_PREFIX + "yourWebContextName");       
    FlowController controller = (FlowController)context.getBean("flowController");
    FlowExecutorImpl flowExecutorImpl = (FlowExecutorImpl)controller.getFlowExecutor();
    FlowDefinitionRegistryImpl flowDefinitionRegistryImpl = (FlowDefinitionRegistryImpl)flowExecutorImpl.getDefinitionLocator();
    //Assuming you have log configured
    log.info("Registered Flow Ids are:"+flowDefinitionRegistryImpl.getFlowDefinitionIds());

FlowController has access to FlowExecutor(initial point of entry for webflow). FlowExecutor has access to flowDefinitionRegistry where all flows are registered before being served to requests.

Hope this helps.