ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Page Navigation in JavaServer Faces
Pages: 1, 2, 3

The Action Class

An Action is an object that performs a task. In JSF, an Action is represented by the abstract javax.faces.application.Action class.



The most important method here is invoke method, an abstract method that returns a String. Its signature is as follows:

public abstract String invoke()

You write the code to perform the task for this Action object in this method. The JSF implementation will call your invoke method.

An Action can be invoked by a UICommand via the actionRef attribute of its command_button or command_hyperlink tags. For an instruction on how to use this class, see the example in the section "Conditional Page Navigation."

Simple Navigation

The following example illustrates the simplest navigation rule. This application consists of two pages: page1.jsp and page2.jsp. From page1.jsp, the user can click the button to go to page2.jsp, and vice versa. The navigation rules in Listing 1 specify the navigation from both page1.jsp and page2.jsp.

<navigation-rule>
    <from-tree-id>/page1.jsp</from-tree-id>

    <navigation-case>
        <to-tree-id>/page2.jsp</to-tree-id>
    </navigation-case>

</navigation-rule>

<navigation-rule>
    <from-tree-id>/page2.jsp</from-tree-id>

    <navigation-case>
        <to-tree-id>/page1.jsp</to-tree-id>
    </navigation-case>

</navigation-rule>

The first navigation-rule element specifies the target for page1.jsp. It contains the navigation-case element with a to-tree-id sub-element, without a from-outcome or a from-action-ref sub-element. This means that, regardless of the outcome, target page (page2.jsp) will be displayed. The second navigation rule specifies the target for page2.jsp. From page2.jsp, the user will definitely go to page1.jsp.

Note: the original page will be re-displayed if there is an error in processing.

Both page1.jsp and page2.jsp use the TestingBean. This bean is registered in the Application Configuration file as follows:

<managed-bean>
    <managed-bean-name>TestingBean</managed-bean-name>

    <managed-bean-class>ch06.TestingBean</managed-bean-class>

    <managed-bean-scope>session</managed-bean-scope>

</managed-bean>

page1.jsp contains a UIInput component and a validator that forces the user to type four or more characters into the UIInput component. The page1.jsp page is given in Listing 6.

Listing 6. page1.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>Page 1</title>
</head>
<body>
<f:use_faces>
<h:form  formName="myForm">
Enter your user name (minimum 4 characters) :
  <h:input_text valueRef="TestingBean.value">
    <f:validate_length minimum="4"/>
  </h:input_text>
<br>
<h:command_button commandName="submit" label="submit"/>
<br>
<h:output_errors/>
</h:form>
</f:use_faces>
</body>
</html>

page2.jsp contains a UIOutput component that displays the value entered by the user to the UIInput component in the page1.jsp page. The page2.jsp page also has a command button for the user to go back to the page1.jsp page. The page2.jsp page is given in Listing 7.

Listing 7. page2.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>Page 2</title>
</head>
<body>
<f:use_faces>
<h:form  formName="myForm">
Your user name is <h:output_text
valueRef="TestingBean.value"/>
<br>
<h:command_button commandName="back" label="Back"/>
</h:form>
</f:use_faces>
</body>
</html>

You can invoke the page1.jsp page using the following URL:

http://localhost:8080/JSFCh06/faces/page1.jsp

Now, type in a string of four or more characters. You should see page2.jsp welcoming you. If you click the Back button, you will see the page1.jsp page again.

Note that if you type less than four characters into the UIInput component in page1.jsp page, the validator in page1.jsp will add a Message object to the FacesContext instance, and this will cause the navigation to be cancelled. As a result, you will see the page1.jsp page again, with an error message.

Pages: 1, 2, 3

Next Pagearrow