Skip to main content

JSF Tip #35 - Create a flow programmatically using annotations

Posted by mriem on November 8, 2013 at 3:31 PM PST

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>

Note the code for this sample is available as part of the Glassfish samples (see the subversion repository at http://svn.java.net/svn/glassfish-samples~svn/trunk/ws/javaee7)
And that is it.

Enjoy!

Related Topics >>