Skip to main content

JSF Tip #41 - How to enter and exit a flow

Posted by mriem on November 18, 2013 at 9:29 AM PST

In JSF 2.2 it is possible to define flows, one of the things you would want to do is to be able enter and to exit a flow. This blog entry shows you a sample on how you can enter a flow and also how to exit it properly.

The page below uses a commandButton to enter the flow "enterexit"

<?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>Enter and Exit Flow sample</title>
    </h:head>
    <h:body>
        <h1>Enter and Exit Flow sample</h1>
        <h:form>
            <h:commandButton action="enterexit" value="Enter"/>
        </h:form>
    </h:body>
</html>

Now that we entered the flow we are going to define the exit point.

<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <flow-definition id="enterexit">
        <flow-return id="taskFlowReturn1">
            <from-outcome>/exit</from-outcome>
        </flow-return>
    </flow-definition>
</faces-config>

We have defined the exit point so now we are going to show you the page that allows you to exit 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>Enter and Exit Flow sample</title>
    </h:head>
    <h:body>
        <h1>Entered flow
        <h:form>
            <h:commandButton action="taskFlowReturn1" value="Exit"/>
        </h:form>
    </h:body>
</html>

And lets verify we actually exited 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>Enter and Exit Flow sample</title>
    </h:head>
    <h:body>
        <h1>Enter and Exit Flow sample</h1>
        <p>
            We exited the flow.
        </p>
        <p>
            Do you want to <a href="#{facesContext.externalContext.requestContextPath}/faces/index.xhtml">Start over</a>
        </p>
    </h:body>
</html>

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

And that is it.

Enjoy!

Related Topics >>