Вы находитесь на странице: 1из 40

19

Responding to Application Events

Copyright 2008, Oracle. All rights reserved.


Objectives
After completing this lesson, you should be able to do the
following:
Configure and use managed beans to contain code to
respond to events
Describe the different types of events
Use phase listeners
Create event listeners
Define action listeners
Create value change listeners
Describe ADF Faces enhanced event handling
List other types of server events used by ADF Faces
components
Explain how ADF components use AJAX events
Use contextual events to coordinate regions
19 - 2 Copyright 2008, Oracle. All rights reserved.
Adding UI Code

Managed beans:
Configured in adfc-config.xml or other task flow .xml
file
Plain Old Java Objects (POJOs), Lists and Maps
Have no-argument constructor
Lazy initialization by JavaServer Faces framework as
needed

19 - 3 Copyright 2008, Oracle. All rights reserved.


Creating Managed Beans

2. Browse for existing Java


1. Right-click command button. class or create new class.

3. Search class.

5. Create method.

4. Select class.

6. Bean automatically gets configured


in adfc-config.xml file (see next slide).

19 - 4 Copyright 2008, Oracle. All rights reserved.


Registering Existing Java Classes
as Managed Beans

Use Overview tab of task flow.

19 - 5 Copyright 2008, Oracle. All rights reserved.


Configuring Managed Beans

Entry in adfc-config.xml:
<managed-bean>
<managed-bean-name> DepartmentBean </managed-bean-name>
<managed-bean-class> sample.DepartmentsManagedBean
</managed-bean-class>
<managed-bean-scope> request </managed-bean-scope>
</managed-bean>

Skeleton for new managed


bean (add your own code):

19 - 6 Copyright 2008, Oracle. All rights reserved.


Referencing Managed Beans

Use in Expression Language on JSF Page:


<af:inputText
value="#{DepartmentBean.firstName}"/>
Use in Java code:
FacesContext facesContext =
FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
ValueExpression exp =
expressionFactory.createValueExpression(elContext,
"#{DepartmentBean}", DepartmentsManagedBean.class);
DepartmentsManagedBean dept =
(DepartmentsManagedBean)exp.getValue(elContext);

19 - 7 Copyright 2008, Oracle. All rights reserved.


Describing JSF and ADF Life-Cycle Roles

The JSF life cycle handles presentation:


Submission of values on the page
Validation for components
Navigation
Displaying the components on the resulting page
Saving and restoring state
The ADF life cycle handles data:
Preparing and updating the data model
Validating the data at the model layer
Executing methods on the business layer

19 - 8 Copyright 2008, Oracle. All rights reserved.


Coordinating JSF and ADF Life Cycles

19 - 9 Copyright 2008, Oracle. All rights reserved.


Specifying When to Refresh Binding Executables

Refreshing iterator reconnects it with RowSetIterator


object.
Refreshing invoke action binding invokes the action.
You can set the Refresh property to determine when to
refresh.

19 - 11 Copyright 2008, Oracle. All rights reserved.


Specifying Whether to Refresh Binding
Executables

Set RefreshCondition to an EL expression evaluating to


true (default) or false.
Example: A search page that initially shows no data has
iterator binding with settings:
RefreshCondition=
"#{!adfFacesContext.isInitialRender}"
(false when page is first rendered)
Refresh="renderModel"

19 - 13 Copyright 2008, Oracle. All rights reserved.


Describing Types of Events

JSF supports:
Phase events
Execute as part of the JSF and ADF life cycle
Can be used to augment standard behavior
Action events
Occur when a command component is activated, such as
when a user clicks a button or a link
Return a control flow outcome
Value change events
Occur when the local value of a input component changes,
such as when a user selects a check box
Used for managing UI elements

19 - 14 Copyright 2008, Oracle. All rights reserved.


Using Phase Listeners

JSF life cycle


Postback

Restore/Create Apply Process


Component Tree Request Events,
(View) Values Validation
Initial
Request

Render Invoke Update


Response Application Model

19 - 15 Copyright 2008, Oracle. All rights reserved.


Using Event Listeners

Event Object
Ive been clicked!
Does anyone care?
Yes Im listening!
Now Ill get to work

Event Listener

Event Source

19 - 16 Copyright 2008, Oracle. All rights reserved.


Responding to Action Events

Command components raise action events.


Code to respond to the action event can be in the backing
bean or external class.
Stub code is generated by JDeveloper on demand.
The action is registered in the page source.
Action events are called in the Invoke Application phase of
the life cycle.
Action methods are the last to execute after other listeners,
such as valueChange events.

19 - 17 Copyright 2008, Oracle. All rights reserved.


Creating Action Methods

To create an action method:


1. Invoke the action binding
editor.
2. Choose existing bean and
method, or create new.
JDeveloper:
If the bean does not exist, creates Java class and registers
as a managed bean
If the method does not exist, creates a method stub in the
managed bean class
Adds the method to the action property of component
Add your own code.
Action methods return a String outcome.

19 - 18 Copyright 2008, Oracle. All rights reserved.


Using Action Listeners

Action listeners differ from action methods:


Action listeners: Action methods:
Contain code to respond Are used for navigation
to an action
Do not return a value Return a String outcome
Execute after value Execute last
change listeners

However, both can be


initiated by the same
action, such as a
button click.

19 - 19 Copyright 2008, Oracle. All rights reserved.


Value Change Events

Input components raise value change events.


Code for the value change listener can be in the backing
bean or external class.
Stub code is generated by JDeveloper.
The value change event is registered in the page source.
Value change events are processed in the Invoke
Application phase of the life cycle.
Value change events fire before action events.

19 - 20 Copyright 2008, Oracle. All rights reserved.


Listening for Value Change Events

Select the input component in the visual editor.


Edit the valueChangeListener property and select a bean
and method, or create a new one.
JDeveloper:
Creates the method in the managed bean if it doesnt already
exist
Adds the method to the valueChangeListener property in
the page source
Add your code.

19 - 21 Copyright 2008, Oracle. All rights reserved.


Event and Listener Execution Order

Following is the order in which the events and listeners fire:


1. Validator (managed bean method that performs validation
on the components value)
2. Value change listener (managed bean method to handle
value-change events)
3. Action listener (managed bean method to handle action
events)
4. Action method
(managed bean
method to return a
logical outcome
String that is used
in navigation)
19 - 22 Copyright 2008, Oracle. All rights reserved.
ADF Faces Enhanced Event Handling

ADF Faces adds to JSF event handling by providing:


Partial page rendering (PPR)
JavaScript

19 - 23 Copyright 2008, Oracle. All rights reserved.


Using JavaScript in ADF Faces Applications

You can use JavaScript:


To create custom components
To respond to events
In individual pages or bundled in a library

19 - 24 Copyright 2008, Oracle. All rights reserved.


Other ADF Faces Server Events

Property Inspector of ADF Faces components displays


appropriate event listeners:

19 - 25 Copyright 2008, Oracle. All rights reserved.


Using Table Model Methods in a Selection
Listener

You can do the


following
programmatically:
Make row current:
setRowIndex()
or setRowKey().
Access data in
the current row:
getRowData().
Obtain number of rows:
getRowCount().
Determine if you are at end
of table: isRowAvailable()

19 - 27 Copyright 2008, Oracle. All rights reserved.


Using Tree Model Methods in a Selection Listener

You can do the


following
programmatically:
Determine if a row
has children:
isContainer()
Access children of
current row:
enterContainer()
Revert to parent
collection:
exitContainer()

19 - 28 Copyright 2008, Oracle. All rights reserved.


Additional AJAX Events

Additional event types include AJAX events:

19 - 29 Copyright 2008, Oracle. All rights reserved.


Characteristics of the Contextual Event
Framework

Contextual events:
Provide a way to coordinate regions

Containing
Page

Can be invoked and consumed only by method action of a


data control
Producer and consumer may be the same or different data
controls.

19 - 30 Copyright 2008, Oracle. All rights reserved.


Contextual Events Overview

Containing page

Event
map

Payload Consumer
method method
Producer Consumer
region region

19 - 31 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions
Payload Handler
Method Method
4
1 3

Producer Containing Consumer


Page

2
5
Producer Containing Consumer
PageDef PageDef PageDef

19 - 32 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 1

Producer Containing Consumer


Page

Define a page
that contains
two regions.

19 - 33 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 2
Construct Payload
an event Method
payload.

Producer Containing Consumer


Page

19 - 34 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 3
Payload Invoke the
Method payload method.

Producer Containing Consumer


Page

19 - 35 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 4
Payload
Method

Producer Containing Consumer


Page

Producer
Supply a value
PageDef for the payload
method argument.

19 - 36 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 5
Payload
Method

Producer Containing Consumer


Page

Producer
PageDef Raise an event
when the
payload method

is invoked.

19 - 37 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 6
Payload Handler Create an
Method Method event handler.

Producer Containing Consumer


Page

Producer
PageDef

19 - 38 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 7
Payload Handler
Method Method

Bind the
event
Producer Containing handler. Consumer
Page

Producer Containing Consumer


PageDef PageDef PageDef

19 - 39 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 8
Payload Handler
Method Method

Producer Containing Consumer


Page

Producer Containing Consumer


PageDef PageDef PageDef
Map the event.

19 - 40 Copyright 2008, Oracle. All rights reserved.


Using the Contextual Event Framework to
Coordinate Page Regions: Step 9
Payload Handler
Method Method

Producer Containing Consumer


Page
Specify producer
region refreshing.

Specify consumer
region refreshing .
Producer Containing Consumer
PageDef PageDef PageDef

19 - 41 Copyright 2008, Oracle. All rights reserved.


Summary

In this lesson, you should have learned how to:


Configure and use managed beans to contain code to
respond to events
Describe the different types of events
Use phase listeners
Create event listeners
Define action listeners
Create value change listeners
Describe ADF Faces enhanced event handling
List other types of server events used by ADF Faces
components
Explain how ADF components use AJAX events
Use contextual events to coordinate regions

19 - 42 Copyright 2008, Oracle. All rights reserved.


Practice 19 Overview:
Responding to Events

This practice covers the following topics:


Defining task flow parameters
Using a contextual event to coordinate user selection in a
tree in one region with the display in another region
Using phase listeners

19 - 43 Copyright 2008, Oracle. All rights reserved.

Вам также может понравиться