Handling Application Events

tacitmarigoldInternet and Web Development

Jan 25, 2014 (3 years and 6 months ago)

190 views

Copyright © 2007, Oracle. All rights reserved.

Handling Application Events

14
-
2

Copyright © 2007, Oracle. All rights reserved.

Objectives

After completing this lesson, you should be able to do
the following:


Define the types of JavaServer Faces (JSF) events


Create event listeners for a JSF application


Describe how the JSF life cycle handles validation


List the types of validation provided by JSF and
Application Development Framework (ADF) Faces

14
-
3

Copyright © 2007, Oracle. All rights reserved.

JSF Event Model


The JSF Event Model is based on the event model
defined by the JavaBeans specification where the
event is represented by a specific class.


An event source object fires an event by calling an
event notification method on event listener
objects registered to receive the event, passing a
reference to the event object as a notification
method argument.


Developers can write their own event listener
implementations, or reference a backing bean
method using Expression Language (EL).

14
-
4

Copyright © 2007, Oracle. All rights reserved.

Types of Events

JSF supports:


Action events


Occur when a command component is activated.
For example, when a user clicks a button or a link.


Return a navigation case


Value change events


Occur when the local value of an input component
changes. For example, when a user selects a check
box.


Are used for managing UI elements


Phase events


Execute as part of the JSF life cycle


Can be used to augment standard behavior

14
-
5

Copyright © 2007, Oracle. All rights reserved.

Action Events


Command components raise action events.


Code 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 application phase
of the life cycle.


Action events are the last to fire after other
listeners (for example, value change events).

14
-
6

Copyright © 2007, Oracle. All rights reserved.

Creating Action Events


Two ways:


Double
-
click a command component in the Visual
Editor.


Enter a method name, including
()
, in the Action
property of the component.


JDeveloper:


Creates a method in the backing bean


Adds the method to the Action property in the page
source


Add your custom code.


Action methods return a string outcome.

14
-
7

Copyright © 2007, Oracle. All rights reserved.

Value Change Events


Input components raise value change events.


Code 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 called in the application
phase of the life cycle.


Value change events fire before action events.

14
-
8

Copyright © 2007, Oracle. All rights reserved.

Creating Value Change Events


Select the input component in the Visual Editor.


Set the
valueChangeListener

property to a
method name that includes
()
.


JDeveloper:


Creates the method in the backing bean


Adds the method to the
valueChangeListener

property in the page source


Add your custom code.

14
-
9

Copyright © 2007, Oracle. All rights reserved.

Event Listener Classes


Action listener:


Is a class that wants to be notified when a
command component fires an action event


Implements
javax.faces.event.ActionListener



Value change listener:


Is a class that wants to be notified when an input
component fires a value change event


Implements
javax.faces.event.ValueChangeListener


14
-
10

Copyright © 2007, Oracle. All rights reserved.

Handling Action Events


To listen for an action event, you need to:


Create a class that implements a
javax.faces.event.ActionListener

interface.


Register the action listener instance on the
component’s
actionListener

attribute.


To register the action listener, drag an action
listener from JSF >

Core to the component.


You can create multiple listeners on each
component.

14
-
11

Copyright © 2007, Oracle. All rights reserved.

Handling Value Change Events


To handle a value change event for an input
component, you need to:


Implement the
ValueChangeListener

interface


Register the value change listener instance on the
component (using the
f:valueChangeListener

tag)


To register the action listener, drag a value change
listener from JSF >

Core to the component.


You can create multiple value change listeners per
component.

14
-
12

Copyright © 2007, Oracle. All rights reserved.

Event and Listener Execution Order

The order in which the events and listeners fire is:


Validators


Value change listeners


Action listeners


Action events

14
-
13

Copyright © 2007, Oracle. All rights reserved.

Validation in the JSF Life Cycle

Apply request values

Process validations:

ADF Faces validation

Successful?

Update model values

Are there ADF

validation

rules?

Render response:

Display either new page,

or same page with errors

Invoke application

Validate model updates:

ADF validation rules

are executed

Successful?

Yes

Yes

Yes

No

No

No

2

8

7

6

5

4

3

1

8

14
-
15

Copyright © 2007, Oracle. All rights reserved.

Entity Validation in the Commit Cycle

Start posting

Start validating

All entity

objects valid?

Post entity objects

Any entity

objects invalid?

Commit changes

Yes

Yes

No

No

14
-
16

Copyright © 2007, Oracle. All rights reserved.

Handling Validation Exceptions


Default behavior:


Exceptions are bundled and returned to client.


If posted, transactions are rolled back.


Error message(s) are displayed on the JSF page.


You can change default behavior by:


Creating custom exception handler


Changing how life cycle reports exceptions


Customizing page exception handling

14
-
17

Copyright © 2007, Oracle. All rights reserved.

Creating Custom Exception Handlers

To create a custom error handler:

1.

Create a class that extends
DCErrorHandlerImpl
.

2.
In the new class, override the
reportException()

method.

3.
Globally override the default error handler by
creating a custom page life cycle class that
extends
FacesPageLifecycle
.

14
-
19

Copyright © 2007, Oracle. All rights reserved.

Changing Life Cycle Exception Reporting

To customize how the life cycle reports errors:

1.

Create a custom page life cycle class that extends
FacesPageLifecycle
.

2.

Override the
reportErrors()

method.

3.

Create a new phase listener that returns the
custom life cycle:

a.

Extend the ADFPhaseListener class.

b.

Override the
createPageLifecycle()

method to
return the new custom life cycle.

c.

Register the phase listener in
faces
-
config.xml

(Overview tab
> Life Cycle > New).

14
-
20

Copyright © 2007, Oracle. All rights reserved.

Customizing Page Exception Handling

To override exception handling for a single page:

1.

Create a custom page life cycle class that extends
FacesPageLifecycle
.

2.

Override the
reportErrors()

method.

3.

Open the page definition for the page.

4.

In the Structure window, select the PageDef node.

5.

In the Property

Inspector, set the

ControllerClass


property to the new

class.

14
-
21

Copyright © 2007, Oracle. All rights reserved.

JavaServer Faces Validators

Validation can be performed through:


Separate validator components (classes), which
provide a high degree of reusability


Backing bean code


Simpler to understand because code is co
-
located
with the subject


Not reusable


JSF Reference Implementation (RI). It provides
default validator components.


No client
-
side validation code, such as JavaScript,
generated


Multiple validators can be used on one input item.

14
-
22

Copyright © 2007, Oracle. All rights reserved.

ADF Binding Validation


ADF Model validation


Validator is declaratively added to the ADF attribute.


Validation rule is enforced on the server.


ADF Faces validation


Validator is declared in the view layer.


Validation rules can be enforced on the client and
the server.

14
-
23

Copyright © 2007, Oracle. All rights reserved.

Additional ADF Faces Validators


ADF Faces provides additional validators:


DateRangeValidator


RegExpValidator
:

Example:
[^0
-
9]{1,10}


ByteLengthValidator


ADF Faces validators support

client
-
side validation through JavaScript.


<af:inputText

value="#{bindings.DepartmentName.inputValue}“ ...>

<af:validateRegExp


noMatchMessageDetail="Value not allowed"


pattern="[^0
-
9]{1,10}"/>

</af:inputText>

14
-
24

Copyright © 2007, Oracle. All rights reserved.

Creating Backing Bean Validation

in JDeveloper

14
-
25

Copyright © 2007, Oracle. All rights reserved.

Backing Bean Validator: Code Example


public void validateDepartmentId(FacesContext
facesContext, UIComponent uiComponent,Object object)
{


String expr = "[^0
-
9]{1,10}";


Pattern p = Pattern.compile(expr);


Matcher m = p.matcher((String)object);

//write message if input is invalid

if (!m.find()){


facesContext.addMessage("RegExError",new
FacesMessage(


FacesMessage.SEVERITY_ERROR, "InvalidValue
Provided for Deptno",null));


facesContext.getApplication().getViewHandler().


restoreView(facesContext,"/Dept.jsp");


}


}

14
-
26

Copyright © 2007, Oracle. All rights reserved.

Input Validation


Use input validation to:


Avoid “garbage

in
,
garbage

out” applications


Block bad user input


Avoid attacks


Avoid misuse of form fields


JavaServer Faces and ADF provide the following
options for validation:


ADF binding validation


JSF validator components


Backing bean “coded” validation

14
-
27

Copyright © 2007, Oracle. All rights reserved.

Summary

In this lesson, you should have learned how to:


Describe how the JSF event model enables
developers to write their own event listener
implementations


Use action events to fire when a component is
activated and value change events when the local
value of an input component has changed


Use managed beans as the state holder of user
input and the component’s data


Describe how the JSF life cycle handles validation


Implement the types of validation provided by JSF
and ADF Faces to create robust applications

14
-
28

Copyright © 2007, Oracle. All rights reserved.

Practice Overview:

Wiring the Application Together

This practice covers the following topics:


Modifying the Search page to call the Edit page


Adding code to set the context from the Search
page


Modifying the Edit page to use the context from
the Search page


Adding the Delete functionality to the application