2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages The Example JavaServer Faces Application Adding UI Components to a Page Using the HTML Component Tags Thestyle andstyleClass Attributes Thevalue andbinding Attributes Rendering a Text Field with theinputText Tag Rendering a Label with theoutputLabel Tag Rendering a Hyperlink with theoutputLink Tag Displaying a Formatted Message with theoutputFormat Tag Rendering a Password Field with theinputSecret Tag Using Command Components for Performing Actions and Navigation Rendering a Button with thecommandButton Tag Rendering a Hyperlink with thecommandLink Tag Using Data-Bound Table Components Adding Graphics and Images with thegraphicImage Tag Laying Out Components with theUIPanel Component Rendering Components for Selecting One Value Displaying a Check Box Using theselectBooleanCheckbox Tag Displaying a Menu Using theselectOneMenu Tag Rendering Components for Selecting Multiple Values TheUISelectItem,UISelectItems, andUISelectItemGroup Components Displaying Error Messages with themessage andmessages Tags Referencing Localized Static Data Converting a Component's Value Registering Listeners on Components Registering a Value-Change Listener on a Component Registering an Action Listener on a Component Validating a Component's Value Binding Component Values and Instances to External Data Sources Binding a Component Value to a Property Binding Converters, Listeners, and Validators to Backing Bean Properties Referencing a Backing Bean Method Referencing a Method That Performs Navigation Referencing a Method That Handles an Action Event Referencing a Method That Performs Validation Referencing a Method That Handles a Value-change Event 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Binding Component Values and Instances to External Data SourcesAs explained inBacking Beans, a component tag can wire its component’s data toa back-end data object by doing one of the following: A component tag’svalue attribute uses a value expression to bind the component’svalue to an external data source, such as a bean property. A componenttag’sbinding attribute uses a value expression to bind a component instance toa bean property. When a component instance is bound to a backing bean property, theproperty holds the component’s local value. Conversely, when a component’s value is bound toa backing bean property, the property holds the value stored in the backingbean. This value is updated with the local value during the update modelvalues phase of the life cycle. There are advantages to both of thesetechniques. Binding a component instance to a bean property has these advantages:
Binding a component’s value to a bean property has these advantages:
In most situations, you will bind a component’s value rather than its instanceto a bean property. You’ll need to use a component binding only whenyou need to change one of the component’s attributes dynamically. For example, ifan application renders a component only under certain conditions, it can set thecomponent’srendered property accordingly by accessing the property to which the component isbound. When referencing the property using the component tag’svalue attribute, you needto use the proper syntax. For example, suppose a backing bean calledMyBeanhas thisint property: int currentOption = null;int getCurrentOption(){...}void setCurrentOption(int option){...}The value attribute that references this property must have this value-binding expression: #{MyBean.currentOption}In addition to binding a component’s value to a bean property, thevalueattribute can specify a literal value or can map the component’s data toany primitive (such asint), structure (such as an array), or collection (suchas a list), independent of a JavaBeans component.Table 11-8 lists some example value-binding expressionsthat you can use with thevalue attribute. Table 11-8 Example Value-binding Expressions
The next two sections explain in more detail how to use thevalue attribute to bind a component’s value to a bean property or otherexternal data sources and how to use thebinding attribute to bind a componentinstance to a bean property. Binding a Component Value to a PropertyTo bind a component’s value to a bean property, you specify the nameof the bean and the property using thevalue attribute. As explained inBacking Beans, the value expression of the component tag’svalue attribute must match the correspondingmanaged bean declaration in the application configuration resource file. This means that the name of the bean in the value expressionmust match themanaged-bean-name element of the managed bean declaration up to the firstperiod (.) in the expression. Similarly, the part of the value expression afterthe period must match the name specified in the correspondingproperty-name element in theapplication configuration resource file. For example, consider this managed bean configuration, which configures theImageArea bean correspondingto the North America part of the image map on thechooselocale.jsp pageof the Duke’s Bookstore application: <managed-bean> <managed-bean-name> NA </managed-bean-name> <managed-bean-class> model.ImageArea </managed-bean-class> <managed-bean-scope> application </managed-bean-scope> <managed-property> <property-name>shape</property-name> <value>poly</value> </managed-property> <managed-property> <property-name>alt</property-name> <value>NAmerica</value> </managed-property> ...</managed-bean> This example configures a bean calledNA, which has several properties, one ofwhich is calledshape. Although thearea tags on thechooselocale.jsp page do not bind toanImageArea property (they bind to the bean itself), to do this, yourefer to the property using a value expression from thevalue attribute ofthe component’s tag: <h:outputText value="#{NA.shape}" />Much of the time you will not include definitions for a managedbean’s properties when configuring it. You need to define a property and itsvalue only when you want the property to be initialized with a valuewhen the bean is initialized. If a component tag’svalue attribute must refer to a property that isnot initialized in themanaged-bean configuration, the part of the value-binding expression afterthe period must match the property name as it is defined in thebacking bean. SeeApplication Configuration Resource File for information on how to configure beans in the application configurationresource file. Writing Bean Properties explains in more detail how to write the backing bean properties foreach of the component types. Binding a Component Value to an Implicit ObjectOne external data source that avalue attribute can refer to is animplicit object. Thebookreceipt.jsp page of the Duke’s Bookstore application includes a reference to animplicit object from a parameter substitution tag: <h:outputFormat title="thanks" value="#{bundle.ThankYouParam}"> <f:param value="#{sessionScope.name}"/></h:outputFormat>This tag gets the name of the customer from the session scopeand inserts it into the parameterized message at the keyThankYouParam from the resourcebundle. For example, if the name of the customer is Gwen Canigetit, thistag will render: Thank you, Gwen Canigetit, for purchasing your books from us. Thename tag on thebookcashier.jsp page has theNameChanged listener implementation registered onit. This listener saves the customer’s name in the session scope when thebookcashier.jsp page is submitted. SeeImplementing Value-Change Listeners for more information on how this listenerworks. SeeRegistering a Value-Change Listener on a Component to learn how the listener is registered on the tag. Retrieving values from otherimplicit objects is done in a similar wayto the example shown in this section.Table 11-9 lists the implicit objects that avalue attribute can refer to. All of the implicit objects except for thescope objects are read-only and therefore should not be used as a valuefor aUIInput component. Table 11-9 Implicit Objects
Binding a Component Instance to a Bean PropertyA component instance can be bound to a bean property using avalue expression with thebinding attribute of the component’s tag. You usually bind acomponent instance rather than its value to a bean property if the beanmust dynamically change the component’s attributes. Here are two tags from thebookcashier.jsp page that bind components to beanproperties: <h:selectBooleanCheckbox rendered="false" binding="#{cashier.specialOffer}" /><h:outputLabel for="fanClub" rendered="false" binding="#{cashier.specialOfferText}" > <h:outputText value="#{bundle.DukeFanClub}" /></h:outputLabel>TheselectBooleanCheckbox tag renders a check box and binds thefanClubUISelectBoolean component tothespecialOffer property ofCashierBean. TheoutputLabel tag binds the component representing the checkbox’s label to thespecialOfferText property ofCashierBean. If the application’s locale isEnglish, theoutputLabel tag renders: I’d like to join the Duke Fan Club, free with my purchase of over $100 Therendered attributes of both tags are set tofalse, which prevents thecheck box and its label from being rendered. If the customer orders morethan $100 (or 100 euros) worth of books and clicks theSubmit button, thesubmit method ofCashierBean sets both components’rendered properties totrue, causing the checkbox and its label to be rendered. These tags use component bindings rather than value bindings because the backing beanmust dynamically set the values of the components’rendered properties. If the tags were to use value bindings instead of component bindings, thebacking bean would not have direct access to the components, and would thereforerequire additional code to access the components from theFacesContext instance tochange the components’rendered properties. Writing Properties Bound to Component Instances explains how to write the bean properties bound to the example componentsand also discusses how thesubmit method sets therendered properties of the components. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |