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 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 a Component Value to an Implicit Object Binding a Component Instance to a Bean 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 | Using the Standard ConvertersThe JavaServer Faces implementation provides a set ofConverter implementations that you can useto convert component data. For more information on the conceptual details of theconversion model, seeConversion Model. The standardConverter implementations, located in thejavax.faces.convert package, are as follows: Each of these converters has a standard error message associated with them. Ifyou have registered one of these converters onto a component on your page,and the converter is not able to convert the component’s value, the converter’serror message will display on the page. For example, the error message thatdisplays ifBigIntegerConverter fails to convert a value is: {0} must be a number consisting of one or more digitsIn this case the{0} substitution parameter will be replaced with the nameof the input component on which the converter is registered. See section 2.4.5of the JavaServer Faces specification, version 1.2, for a complete list of errormessages. Two of the standard converters (DateTimeConverter andNumberConverter) have their own tags,which allow you to configure the format of the component data using thetag attributes.UsingDateTimeConverter discusses usingDateTimeConverter.UsingNumberConverter discusses usingNumberConverter. The following sectionexplains how to convert a component’s value including how to register the otherstandard converters with a component. Converting a Component’s ValueTo use a particular converter to convert a component’s value, you need toregister the converter onto the component. You can register any of the standardconverters on a component in one of four ways:
As an example of the second approach, if you want a component’s datato be converted to anInteger, you can simply bind the component’s valueto a property similar to this: Integer age = 0;public Integer getAge(){ return age;}public void setAge(Integer age) {this.age = age;}If the component is not bound to a bean property, you canemploy the third technique by using theconverter attribute on the component tag: <h:inputText converter="javax.faces.convert.IntegerConverter" /> This example shows theconverter attribute referring to the fully-qualified class name ofthe converter. Theconverter attribute can also take the ID of thecomponent. If the converter is a custom converter, the ID is defined inthe application configuration resource file (seeApplication Configuration Resource File). The data corresponding to this exampleinputText tag will be converted to ajava.lang.Integer.Notice that theInteger type is already a supported type of theNumberConverter. If you don’t need to specify any formatting instructions using theconvertNumbertag attributes, and if one of the other converters will suffice, you cansimply reference that converter using the component tag’sconverter attribute. Finally, you can nest aconverter tag within the component tag and useeither the converter tag’sconverterId attribute or itsbinding attribute to reference theconverter. TheconverterId attribute must reference the converter’s ID. Again, if the converter isa custom converter, the value ofconverterID must match the ID in the applicationconfiguration resource file; otherwise it must match the ID as defined in theconverter class. Here is an example: <h:inputText value="#{LoginBean.Age}" /> <f:converter converterId="Integer" /></h:inputText>Instead of using theconverterId attribute, theconverter tag can use thebinding attribute. Thebinding attribute must resolve to a bean property that accepts andreturns an appropriateConverter instance. SeeBinding Converters, Listeners, and Validators to Backing Bean Properties for more information. UsingDateTimeConverterYou can convert a component’s data to ajava.util.Date by nesting theconvertDateTime taginside the component tag. TheconvertDateTime tag has several attributes that allow you tospecify the format and type of the data.Table 11-5 lists the attributes. Here is a simple example of aconvertDateTime tag from thebookreceipt.jsp page: <h:outputText id= "shipDate" value="#{cashier.shipDate}"> <f:convertDateTime dateStyle="full" /></h:outputText>When binding theDateTime converter to a component, ensure that the backing beanproperty to which the component is bound is of typejava.util.Date. In thecase of the preceding example,cashier.shipDate must be of typejava.util.Date. Here is an example of a date and time that the precedingtag can display: Saturday, Feb 22, 2003 You can also display the same date and time using this tag: <h:outputText value="#{cashier.shipDate}"> <f:convertDateTime pattern="EEEEEEEE, MMM dd, yyyy" /></h:outputText>If you want to display the example date in Spanish, you canuse thelocale attribute: <h:inputText value="#{cashier.shipDate}"> <f:convertDateTime dateStyle="full" locale="Locale.SPAIN" timeStyle="long" type="both" /></h:inputText>This tag would display sabado 23 de septiembre de 2006 Please refer to theCustomizing Formats lesson of the Java Tutorial athttp://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html formore information on how to format the output using thepattern attribute oftheconvertDateTime tag. Table 11-5convertDateTime Tag Attributes UsingNumberConverterYou can convert a component’s data to ajava.lang.Number by nesting theconvertNumbertag inside the component tag. TheconvertNumber tag has several attributes that allowyou to specify the format and type of the data.Table 11-6 lists theattributes. Thebookcashier.jsp page of Duke’s Bookstore uses aconvertNumber tag to display thetotal prices of the books in the shopping cart: <h:outputText value="#{cart.total}" > <f:convertNumber type="currency"/></h:outputText>When binding theNumber converter to a component, ensure that the backing beanproperty to which the component is bound is of primitive type or hasa type ofjava.lang.Number. In the case of the preceding example,cart.total isof typejava.lang.Number. Here is an example of a number this tag can display $934 This number can also be displayed using this tag: <h:outputText value="#{cart.Total}" > <f:convertNumber pattern="$####" /></h:outputText>Please refer to theCustomizing Formats lesson of the Java Tutorial athttp://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html formore information on how to format the output using the pattern attribute oftheconvertNumber tag. Table 11-6convertNumber Attributes
Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |