JSF Tip #35 - Create a flow programmatically using annotations

Wondering how you programmatically create a flow and use it?

  1. Create the Java annotated flow
  2. Create the pages
    
    package flow;

    import java.io.Serializable;
    import javax.enterprise.inject.Produces;
    import javax.faces.flow.Flow;
    import javax.faces.flow.builder.FlowBuilder;
    import javax.faces.flow.builder.FlowBuilderParameter;
    import javax.faces.flow.builder.FlowDefinition;

    public class EnterFlow implements Serializable {

        private static final long serialVersionUID = -7623501087369765218L;

        @FlowDefinition
        @Produces
        private Flow defineFlow(@FlowBuilderParameter FlowBuilder flowBuilder) {
            FlowBuilder builder = flowBuilder.id("", "flow");
            builder.viewNode("enter", "/enterflow/stepA.xhtml").markAsStartNode();
            Flow flow = builder.getFlow();
            return flow;
        }
    }
        

And the page to enter the flow

    
    <?xml version='1.0' encoding='UTF-8' ?>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">
        <h:head>
            <title>Annotated Enter sample</title>
        </h:head>
        <h:body>
            <h1>Annotated Enter sample</h1>
            <p>
                To enter the flow we use the 'flow' id as defined in 'EnterFlow.java'.
            </p>
            <h:form>
                <h:commandButton action="flow" value="Enter"/>
            </h:form>
        </h:body>
    </html>
        

And the first page in the flow


    <?xml version='1.0' encoding='UTF-8' ?>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">
        <h:head>
            <title>Step A</title>
        </h:head>
        <h:body>
            <h1>Entered flow using start node (which is stepA.xhtml)</h1>
            <h:form>
                <h:commandButton action="stepB" value="Step B"/>
            </h:form>
        </h:body>
    </html>
        

Posted November 8, 2013

Up