jsfparametersjsf-2.2faces-flow

Flow depending on external parameter?


I'm building a simple POC to experiment with Faces Flow.

Behing the scenes I've got a @FlowScoped("addNewUsertoCompanyFlow") bean MyFlowBean.

In its @PostConstruct method, the MyFlowBean needs to fetch an object corresponding to the company A from a service (@Inject).

What's the right way to let MyFlowBean know about the ID of the company A so that it can fetch it from the service ?

Thanks.


Solution

  • Ok, I came up with a solution. The key was not to use the flow backing bean @PostConstruct but rather use the flow initializer, where I can grab request parameters.

    So I'm using some additional input in the form that will start my flow:

    <h:form id="myForm" prependId="false">
        <h:commandLink value="Enter myFlow" action="my-flow"/>
        <h:inputHidden id="parameter" name="parameter" value="8"/>
    </h:form>
    

    In my flow definition I've defined an initializer for the flow, calling some method in the flow backing bean

    @Produces @FlowDefinition
    public Flow defineFlow(@FlowBuilderParameter FlowBuilder flowBuilder) {
    String flowId = "my-flow";
        flowBuilder.id("", flowId);
        flowBuilder.initializer("#{myFlowBean.startFlow()}");
        ...
    }
    

    I've then grabbed the parameter inside the backing bean.

    @Named
    @FlowScoped("my-flow")
    public class MyFlowBean implements Serializable {
    
        public void startFlow() {
            String parameter = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("parameter");
            //now do sthg with the parameter, such as fetching data from an injected service
            ....
        }
    }
    

    Of course it's also possible to do that at the flow definition level

    flowBuilder.initializer("#{trainingFlowBean.startFlow(param['parameter'])}"); 
    

    and just have a parameter in the startFlow method

    public void startFlow(String parameter) {
        ...
    }