26
14 Copyright © 2007, Oracle. All rights reserved. Handling Application Events

14_Handling Application Events

Embed Size (px)

Citation preview

Page 1: 14_Handling Application Events

14Copyright © 2007, Oracle. All rights reserved.

Handling Application Events

Page 2: 14_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

Page 3: 14_Handling Application Events

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).

Page 4: 14_Handling Application Events

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

Page 5: 14_Handling Application Events

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).

Page 6: 14_Handling Application 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.

Page 7: 14_Handling Application Events

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.

Page 8: 14_Handling Application 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.

Page 9: 14_Handling Application Events

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

Page 10: 14_Handling Application Events

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.

Page 11: 14_Handling Application Events

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.

Page 12: 14_Handling Application Events

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

Page 13: 14_Handling Application 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 ADFvalidation

rules?

Render response:Display either new page, or same page with errors

Invoke application

Validate model updates:ADF validation rulesare executed

Successful?

Yes

Yes

Yes

NoNo

No

2

8

7

6

5

4

3

1

8

Page 14: 14_Handling Application Events

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

Entity Validation in the Commit Cycle

Start posting

Start validating

All entityobjects valid?

Post entity objects

Any entityobjects invalid?

Commit changes

Yes

Yes

No

No

Page 15: 14_Handling Application Events

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

Page 16: 14_Handling Application Events

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.

Page 17: 14_Handling Application Events

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).

Page 18: 14_Handling Application Events

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.

Page 19: 14_Handling Application Events

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.

Page 20: 14_Handling Application Events

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.

Page 21: 14_Handling Application Events

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>

Page 22: 14_Handling Application Events

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

Creating Backing Bean Validation in JDeveloper

Page 23: 14_Handling Application Events

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");

} }

Page 24: 14_Handling Application Events

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

Page 25: 14_Handling Application Events

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

Page 26: 14_Handling Application Events

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