jsf-2.2faces-flow

How to send data to a faces-flow?


My use case: the user choose a questionnaire in a form. When the form is submitted, a faces-flow is started to display the questions of the questionnaire.

To send the questionnaire to the flow, in the bean of the flow I inject the CDI bean of the page which contains the form.

I wonder if there are other ways to send the questionnaire to the flow. If there are several ways, what's the best one?


Solution

  • You can pass parameters via the form and get them in the initializer method called at the initialization of your flow.

    Form (just replace the inputHidden parameter with whatever you're using to select your questionnaire)

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

    Flow

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

    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 the questionnaire
        ....
        }
    }
    

    See this answer for more details