10 - Planning Navigation and Page Flow

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

10

Planning Navigation and


Page Flow

Copyright © 2007, Oracle. All rights reserved.


Objectives

After completing this lesson, you should be able to do


the following:
• Use a JavaServer Faces (JSF) Navigation diagram
to plan the pages of an application and the
navigation between them
• Describe JSF Navigation
• Describe the types of JSF Navigation cases
• Create a backing bean for a JSF page

10-2 Copyright © 2007, Oracle. All rights reserved.


Traditional Navigation

In traditional navigation, a button or hyperlink is used


to navigate to a specific page.

<body>
<form action="myNewPage.html">
<button type="button"/>
</form>
<a href="http://myNewPage.html">
Go To My New Page
</a></body>

10-3 Copyright © 2007, Oracle. All rights reserved.


What Is JSF Navigation?

• JSF Navigation is defined through rules.


• Rules use outcomes to determine which page is
displayed on a User Interface (UI) event.
• Rules can be defined as:
– Static
– Dynamic
• They are defined in faces-config.xml.

10-4 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation: Example

firstPage
myNewPage
Button success

firstPage.jsp myNewPage.jsp

JSF Navigation Rules

10-5 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation Rules

• Navigation rules are stored in faces-config.xml.


• Navigation rules can be defined using:
– The faces-config console
– The Pageflow diagrammer
– The faces-config.xml file directly

10-6 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation Rules

Example:
– /login.jsp navigation rule with two cases
– login.jsp managed by the login backing bean
– loginAction() is the method that returns
success or failure:
– If the method returns
success, forward the user to
/successPage.jsp.
– If the method returns
failure, forward the
user to /failurePage.jsp.

10-7 Copyright © 2007, Oracle. All rights reserved.


faces-config Console

10-8 Copyright © 2007, Oracle. All rights reserved.


faces-config.xml


<navigation-rule>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/successPage.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/failurePage.jsp</to-view-id>
</navigation-case>
</navigation-rule>

10-9 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation Modeler
You create a JSF Navigation Diagram by dragging and
dropping elements from the Component Palette. The
faces-config.xml diagram is automatically updated.

10-10 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation Diagram

With the JSF Navigation Diagrammer, you can visually


design your application from a birds-eye view.

10-11 Copyright © 2007, Oracle. All rights reserved.


Navigation Elements

• <navigation-rule> • <from-action>
• <from-view-id> • <from-outcome>
• <navigation-case> • <to-view-id>

<navigation-rule>
<from-view-id>/SREdit.jspx</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/SRSearch.jspx</to-view-id>
</navigation-case>
</navigation-rule>

10-12 Copyright © 2007, Oracle. All rights reserved.


10-13 Copyright © 2007, Oracle. All rights reserved.
Global Rules

Global rules apply to all pages in an application.


• Examples: Help page, Home page, Contact page
• Define the <from-view-id> with an asterisk (*)
wildcard.
• Define <from-outcome> (help).
• Define <to-view-id> (help.jsp).
• Any page in the application that returns help
displays help.jsp.

10-14 Copyright © 2007, Oracle. All rights reserved.


Pattern-Based Rules

• Pattern-based rules are similar to global rules.


• Include a pattern for the <from-view-id> element:
<from-view-id>/staffPages/*
• This rule is used by all pages in the /staffPages
path.
<navigation-rule>
<from-view-id>/staffPages/*</from-view-id>
<navigation-case>
<from-outcome>help</from-outcome>
<to-view-id>/menu/staffHelp.jsp</to-view-id>
</navigation-case>
</navigation-rule>

10-15 Copyright © 2007, Oracle. All rights reserved.


JSF: Example

Sample JSF login application:

10-16 Copyright © 2007, Oracle. All rights reserved.


JSF: Example

A simple example of a JSF Login Application:

loginAction Success

success.jsp
login.jsp

Failure

failure.jsp

10-17 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation: Example

UI components and value binding to a managed bean:

<h:InputText value="#{Login.userId}"/>
<h:InputSecret value="#{Login.password}"/>

Submit <h:commandButton action="#{Login.login}"/>

login.jsp

Login: A managed bean with the userId and


password fields and the login()method

10-18 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation: Example

A command UI component bound to an action:


<h:commandButton action="#{Login.loginAction}"/>

login.jsp

Login: A managed bean (Login.java) with an action


method called loginAction()

Returns string: failure or success

10-19 Copyright © 2007, Oracle. All rights reserved.


JSF Navigation: Example

public String loginAction() {


String userId =
(String)this.getUserId().getValue();
String password =
(String)this.getPassword().getValue();
if (userId.equalsIgnoreCase("gary") &
password.equalsIgnoreCase("test")) {
return "success"; }
return "failure";
}

10-20 Copyright © 2007, Oracle. All rights reserved.


Using the JSF Configuration Editor

10-21 Copyright © 2007, Oracle. All rights reserved.


Managed Beans

• Managed beans can:


– Store state
– Execute a Java routine
– Define a handler for event listeners
• They have no-argument constructors.
• Lazy initialization is performed by JavaServer
Faces.
• Access scope: None, Request, Application, or
Session
• Backing beans:
– Are special types of managed beans
– Contain getter and setter methods for UI
components

10-22 Copyright © 2007, Oracle. All rights reserved.


10-23 Copyright © 2007, Oracle. All rights reserved.
Creating Managed Beans

Entry in faces-config.xml:
<managed-bean>
<managed-bean-name> UserInfo </managed-bean-name>
<managed-bean-class> org.srdemo.view.backing.UserInfo
</managed-bean-class>
<managed-bean-scope> session </managed-bean-scope>
</managed-bean>

10-24 Copyright © 2007, Oracle. All rights reserved.


Managed Bean: Example

• Definition:
<managed-bean>
<managed-bean-name>userbean</managed-bean-name>
<managed-bean-class>com.oracle.sample.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

• Usage on a page:
<h:inputText value="#{userbean.name}"/>

• Usage in code:
Application app =
FacesContext.getCurrentInstance().getApplication();
ValueBinding bind = app.createValueBinding("#{userbean}");
User user = (User)bind.getValue(ctx);

10-25 Copyright © 2007, Oracle. All rights reserved.


Setting Managed Bean Scope

Each managed bean has a


scope:
• Application
• Session
• Request
• None

10-26 Copyright © 2007, Oracle. All rights reserved.


Relationships Between Managed Beans

Managed beans can access other beans:


Beans in Access beans in scope
scope
none application session request

none Yes No No No

application Yes Yes No No

session Yes Yes Yes No

request Yes Yes Yes Yes

10-27 Copyright © 2007, Oracle. All rights reserved.


Managed Properties

• Managed bean variables that are exposed through


getter and setter methods
• Configured in faces-config.xml
<!ELEMENT managed-property (description*, display-
name*, icon*, property-name, property-class?,
(map-entries|null-value|value|list-entries))>

• Possible values:
– Null
– Literal string
– Lists and maps
– Value binding expression (EL)

10-28 Copyright © 2007, Oracle. All rights reserved.


Managed Properties: Examples

Literal string:
<managed-bean> …
<managed-property>
<property-name>label</property-name>
<value>Hello World</value>
</managed-property>

EL accessing a managed bean:


<managed-bean> …
<managed-property>
<property-name>label</property-name>
<value>#{UserInfo['firstname']}</value>
</managed-property>

10-29 Copyright © 2007, Oracle. All rights reserved.


Managed Properties: Examples

Populating a list:
<managed-bean>

<managed-property>
<property-name>bookmarks</property-name>
<list-entries>
<value>http://otn.oracle.com/products/jev</value>
<value>http://otn.oracle.com/products/ias</value>

</list-entries>
</managed-property>

Use <map-entries> to specify managed properties


that are of the Map type.

10-30 Copyright © 2007, Oracle. All rights reserved.


Using the Managed Bean in the JSF Page

Access UI components and values

Login.java managed bean:



Getter for userId
Setter for userId

successPage.jsp:

<h:outputText value="#{Login.userId.value}"

binding="#{SuccessPage.outputText1}"
id="outputText1"
/> …

10-31 Copyright © 2007, Oracle. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Use a JSF Navigation Diagram to plan the pages of
an application and the navigation between them
• Describe JSF Navigation
• Define types of JSF Navigation cases
• Create a backing bean for a JSF page

10-32 Copyright © 2007, Oracle. All rights reserved.


Practice Overview:
Developing JSF Applications

This practice covers the following topics:


• Using a JSF Navigation Diagram to model page
flow in an application
• Creating the JSF pages of the application
• Adding a managed bean to the project

10-33 Copyright © 2007, Oracle. All rights reserved.


10-34 Copyright © 2007, Oracle. All rights reserved.

You might also like