2

My question for today is: is it possible to start the faces flow without using h:commandButton component? In my particular case I would like to use the h:selectOneMenu component to start the particular flow based on the value selected by the user.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jigga
  • 175
  • 8
  • What have you tried? Using Ajax support with a `selectOneMenu`, I don't see why it wouldn't be possible to start a flow like you do with `h:commandButton`. – LaurentG Nov 10 '13 at 07:09

1 Answers1

6

The answer is yes, but with a bit of tweaking. To enter a flow, it is required to create a navigation outcome that equals the flows id. UICommand components (like h:commandButton and h:commandLink) can do that but UIInput components can not (they lack the "action" attribute). However, navigation may be triggered programmatically e.g. by using a ValueChangeListener:

    <h:form>
        <h:selectOneMenu value="#{requestScope.selectedFlow}">
            <f:selectItem itemLabel="--- Select a Flow ---" noSelectionOption="true" />
            <f:selectItem itemLabel="Flow A" itemValue="flow-a" />
            <f:selectItem itemLabel="Flow B" itemValue="flow-b" />
            <f:valueChangeListener type="example.NaviagtionTargetListener" />
            <f:ajax execute="@form" render="@all"/>
        </h:selectOneMenu>           
    </h:form>

The corresponding ValueChangeListener:

public class NaviagtionTargetListener implements ValueChangeListener {

    @Override
    public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
        String target = (String) event.getNewValue();
        ConfigurableNavigationHandler nh =  (ConfigurableNavigationHandler) FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
        nh.performNavigation(target);
    }

}

I created an example on GitHub[1] and wrote a blogpost about the usage of FacesFlow[2]

[1] https://github.com/tasel/facesflow-example

[2] http://blog.oio.de/2014/02/12/a-comprehensive-example-of-jsf-faces-flow/

tasel
  • 629
  • 5
  • 15
  • Damn it! Can't even upvote Your answer due to my poor reputation, but anyways thank you for the clear example. – jigga Feb 13 '14 at 15:47
  • You are welcome :) Though upvoting may require more rep, you can 'accept' the answer – tasel Feb 14 '14 at 07:08
  • Instead of "unsafe" casting of `NavigationHandler`, you can call `FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, target)` that effectively does the same thing. – czerny Mar 09 '15 at 00:25