Академический Документы
Профессиональный Документы
Культура Документы
Objectives
After completing this lesson, you should be able to do the following: Define key JavaServer Faces (JSF) terms Describe the JSF architecture with Application Development Framework (ADF) Differentiate between managed and backing beans Create and reference managed beans Set and use managed properties Use context objects to access the application
7-2
Agenda
Key Terms and Concepts Expression Language (EL) Managed Beans and Properties
7-3
JSF Review
JSF simplifies Web development because it: Is component-based, not markup-based Offers automatic event and state handling Can be used for a diverse client base, and not just HTML Is designed with tooling in mind Is applicable to a wide spectrum of programmer types
7-4
Key Terms
UI component: JSF is component based. Managed bean: It includes objects maintained by the JSF inversion of control mechanism. Expression Language: It holds the key to bind the data. Life cycle: It involves the process of how JSF works.
7-5
JSF Architecture
Markup
Device renderer
JSF Page
UI component Expr. language Managed bean
RDBMS
7-6
7-7
7-8
Event
consumes
UI Component
contains
Contained in
View
Event listener
manipulates executes selects
Converter
generates
Validator
generates
Model objects
specialized
Action method
outcome
Navigation
7-9
JSF Suggestions
Use only JSF:
Use components, not HTML, for creating the layout. Exploit the rendered property for conditional display.
Manage all (non-model) states through JSF managed beans; try not to access the session or request directly.
FacesContext fctx = FacesContext.getCurrentInstance(); Application application = fctx.getApplication(); ELContext elctx = fctx.getELContext(); ExpressionFactory exprfactory = application.getExpressionFactory(); ValueExpression valueExpression = exprfactory.createValueExpression(elctx,"#{mybean}",Object.class); MyBean myBean = (MyBean) valueExpression.getValue(elctx);
7 - 10
Agenda
Key Terms and Concepts Expression Language (EL) Managed Beans and Properties
7 - 11
Managed properties
Java code
FacesContext fctx = FacesContext.getCurrentInstance(); Application application = fctx.getApplication(); ELContext elctx = fctx.getELContext(); ExpressionFactory exprfactory = application.getExpressionFactory(); ValueExpression valueExpression = exprfactory.createValueExpression(elctx,"#{userbean}",Object.class); User user = (User) valueExpression.getValue(elctx);
7 - 12
#{param.somevar}: Value of the request parameter named somevar #{requestScope.somevar}: Value of the request attribute named somevar #{sessionScope.somevar}: Value of the session attribute named somevar #{cookie.somevar}: Value of the cookie named somevar
7 - 13
#{resources.rowLockedError}: The entry with the rowLockedError key in the Map named resources #{user[name]}: The user bean property whose name is given by the value of the name property #{user.addresses[0]}: The first element in the addresses collection property of user #{bindings}: A scoped variable named bindings #{bindings.empTable.labels}: The labels property of the entry with the empTable key in the Map named bindings
Copyright 2008, Oracle. All rights reserved.
7 - 14
Code example: JSP value attribute references EL that gets the total price from the session-scoped bean named cart. The JSP engine evaluates the expression, converts it, and passes the returned value to the tag handler. Immediate evaluation expressions are always read-only value expressions. The example can only get the total price from the cart bean; it cannot set the total price.
7 - 15
Code example: JSF value attribute references EL that points to the name property of the customer bean.
First request: #{customer.name} evaluated during the Render Response phase of the life cycle. EL gets the value of name from the customer bean, as is done in the immediate evaluation. Postback: JSF evaluates the expression at different phases of the life cycle, during which the value is retrieved from the request, validated, and propagated to the customer bean.
Deferred EL can be value expressions that can be used to read and write data.
Copyright 2008, Oracle. All rights reserved.
7 - 16
Agenda
Key Terms and Concepts Expression Language (EL) Managed Beans and Properties
7 - 17
Managed Beans
Managed beans are optional and can be used to:
Hold presentation and navigation logic Store state (for example, information about the authenticated user) Execute a Java routine (for example, by clicking a button) Define a handler for event listeners
Managed beans are not the same as backing beans. Backing beans are a specialized type of managed bean that:
Contain getter and setter methods for UI components Have a one-to-one relationship with a page
7 - 18
7 - 19
Usage on a page:
<h:inputText value="#{userbean.name}"/>
FacesContext fctx = FacesContext.getCurrentInstance(); Application application = fctx.getApplication(); ELContext elctx = fctx.getELContext(); ExpressionFactory exprfactory = application.getExpressionFactory(); ValueExpression valueExpression = exprfactory.createValueExpression(elctx,"#{userbean}",Object.class); User user = (User) valueExpression.getValue(elctx);
7 - 20
7 - 21
Hard-Coded Access
Both value binding and variable resolvers hard code values.
FacesContext fctx = FacesContext.getCurrentInstance(); Application application = fctx.getApplication(); ELContext elctx = fctx.getELContext(); ExpressionFactory exprfactory = application.getExpressionFactory(); ValueExpression valueExpression = exprfactory.createValueExpression(elctx,"#{bank}",Object.class); BankBean bank = (BankBean) valueExpression.getValue(elctx);
7 - 22
<managed-bean> <managed-bean-name>Person</managed-bean-name> <managed-bean-class>demo.PersonBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>bank</property-name> <property-class>demo.BankBean</property-class> <value>#{bank}</value> </managed-property> <managed-bean> <managed-bean> <managed-bean-name>bank</managed-bean-name> <managed-bean-class>demo.BankBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-bean>
7 - 23
Managed Properties
Managed properties: Are managed bean variables that are exposed through getter and setter methods Are configured in faces-config.xml Can take the following possible values:
Null Literal string Lists and maps Value-binding Expression Language (EL)
7 - 24
7 - 25
The bindings object is created by the ADF servlet filter that is defined in the web.xml file. The ADF servlet filter is mapped to *.jsp and *.jspx file types. The bindings object needs to be configured as a managed property before it can be used in managed beans.
7 - 26
JDeveloper will do this automatically if you let it maintain backing beans for you.
7 - 27
FacesContext fctx = FacesContext.getCurrentInstance(); Application application = fctx.getApplication(); ELContext elctx = fctx.getELContext(); ExpressionFactory exprfactory = application.getExpressionFactory(); ValueExpression valueExpression = exprfactory.createValueExpression (elctx,"#{bindingsSomeAttrBind.inputvalue}"}",Object.class); Object myValue = valueExpression.getValue(elctx);
7 - 28
BindingContainer bindings = getBindings(); OperationBinding operationBinding = bindings.getOperationBinding("First"); Object result = operationBinding.execute(); OperationBinding operationBinding = getBindings().getOperationBinding("findDepartmentManagerId"); Map params = operationBinding.getParamsMap(); params.put("searchTerm","Sales"); Number deptManager = operationBinding.execute();
7 - 29
7 - 31
01. OperationBinding operationBinding = (OperationBinding)bindings.getOperationBinding (addItemToCart"); 02. Map params = operationBinding.getParamsMap(); 03. params.put(productId,); 04. Object result = operationBinding.execute();
7 - 32
7 - 33
Context Objects
FacesContext:
Is a static class that provides access to information such as application messages, render kits, ViewRoots, ExternalContext, and so on Represents the current request that is processed Provides access to the world outside JSF Is a context object to support ADF Facesspecific functionality:
Partial page rendering (PPR) Skinning Oracle Help Dialog framework support
ExternalContext:
ADFFacesContext:
7 - 34
Prune the contents of the backing bean; code interesting objects only. Place backing beans in the request scope. Use consistent naming and packaging.
7 - 35
Summary
In this lesson, you should have learned how to: Define key JSF terms Describe the JSF architecture with ADF Differentiate between managed and backing beans Create and reference managed beans Set and use managed properties Use context objects to access the application
7 - 36
7 - 37