Question

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?

Était-ce utile?

La 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

Autres conseils

Addition to "thomas.g"s helpful answer:

I had the same problem, but could not fix it with the hiddenInput approach. Despite the prependId="false" attribute my id and name of the hidden input field got changed by the primefaces p:dataTable element I used. The issue could be fixed with the f:param elemtent inside the h:commandLink element:

<h:commandLink value="Enter myFlow" action="my-flow" >
    <f:param name="parameter" value="8"/>
</h:commandLink>

I hope this might be helpfull to someone with a similar problem.

This can be done in the flow xml file using the initializer tag

   <initializer>
       #{myFlowBean.startFlow()}
   </initializer>

to call the your initialize method in the flow scoped bean

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top