2

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
user1643352
  • 2,635
  • 2
  • 19
  • 25
  • FYI, there are really only a few articles on how to deal with Faces Flow, so I created one that hopefully covers the basic questions: http://blog.oio.de/2014/02/12/a-comprehensive-example-of-jsf-faces-flow/ – tasel Feb 13 '14 at 08:26

3 Answers3

2

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

Community
  • 1
  • 1
thomas.g
  • 3,894
  • 3
  • 29
  • 36
  • I am trying to make it work with a flow defined in an XML file but I can't define an initializer by : the method is never executed. Did you try it? – user1643352 May 10 '14 at 20:09
  • In your other answer you reference at the end of this answer, you use an initializer with a parameter but in the javadoc it is written that the expression to invoke for an initializer, must reference a zero-argument method. – user1643352 May 10 '14 at 20:15
  • Sorry I've never tried to define a flow using an XML file. – thomas.g May 12 '14 at 00:06
  • Concerning the initializer, you're right about the javadoc. Though it did worked for me under WildFly, you'd better stick with the zero-argument method example ;-) – thomas.g May 12 '14 at 00:09
  • I have tried your solution with the definition in a class and it works well. Thanks a lot. I will study why I have this problem with the definition in an XML file. – user1643352 May 12 '14 at 05:43
  • Is there a comparable method for returning values from the flow? – Jon B Sep 25 '14 at 15:25
0

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.

markusf1
  • 333
  • 2
  • 10
0

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