问题
I'm building a simple POC to experiment with Faces Flow.
- page 1: displays a list of companies. The user selects company A then goes to page 2.
page 2: on the selected company page, the user clicks a commandLink to start a wizard to create a new employee to be added to the company A.
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.
回答1:
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) {
...
}
来源:https://stackoverflow.com/questions/21183945/flow-depending-on-external-parameter