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 JavaServer Faces Technology User Interface JavaServer Faces Technology Benefits What Is a JavaServer Faces Application? A Simple JavaServer Faces Application Steps in the Development Process Mapping theFacesServlet Instance Registering a Validator on a Text Field Adding Managed Bean Declarations User Interface Component Model User Interface Component Classes The Life Cycle of a JavaServer Faces Page Further Information about JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 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 | Backing BeansA typical JavaServer Faces application includes one or more backing beans, each ofwhich is a JavaServer Faces managed bean that is associated with the UIcomponents used in a particular page. Managed beans are JavaBeans components (seeJavaBeans Components)that you can configure using the managed bean facility, which is described inConfiguring Beans. This section introduces the basic concepts on creating, configuring, and using backingbeans in an application. Creating a Backing Bean ClassIn addition to defining a no-arg constructor, as all JavaBeans components must do,a backing bean class also defines a set of UI component properties andpossibly a set of methods that perform functions for a component. Each of the component properties can be bound to one of thefollowing:
The most common functions that backing bean methods perform include the following:
As with all JavaBeans components, a property consists of a private data fieldand a set of accessor methods, as shown by this code fromthe Guess Number example: Integer userNumber = null;...public void setUserNumber(Integer user_number) { userNumber = user_number; }public Integer getUserNumber() { return userNumber;}public String getResponse() { ...}Because backing beans follow JavaBeans component conventions, you can reference beans you’ve alreadywritten from your JavaServer Faces pages. When a bean property is bound to a component’s value, it canbe any of the basic primitive and numeric types or any Java objecttype for which the application has access to an appropriate converter. For example,a property can be of typeDate if the application has access toa converter that can convert theDate type to a String and back again.SeeWriting Bean Properties for information on which types are accepted by which component tags. When a bean property is bound to a component instance, the property’s typemust be the same as the component object. For example, if aUISelectBoolean is bound to the property, the property must accept and returnaUISelectBoolean object. Likewise, if the property is bound to a converter, validator, or listener instancethen the property must be of the appropriate converter, validator, or listener type. For more information on writing beans and their properties, seeWriting Bean Properties. Configuring a BeanJavaServer Faces technology supports a sophisticated managed bean creation facility, which allows applicationarchitects to do the following:
An application architect configures the beans in the application configuration resource file. Tolearn how to configure a managed bean, seeConfiguring Beans. The managed beanconfiguration used by the Guess Number example is the following: <managed-bean> <managed-bean-name>UserNumberBean</managed-bean-name> <managed-bean-class> guessNumber.UserNumberBean </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>minimum</property-name> <property-class>long</property-class> <value>0</value> </managed-property> <managed-property> <property-name>maximum</property-name> <property-class>long</property-class> <value>10</value> </managed-property></managed-bean> The JavaServer Faces implementation processes this element on application startup time. WhenUserNumberBeanis first referenced from the page, the JavaServer Faces implementation initializes it andsets the values of the properties,maximum andminimum. The bean is then storedin session scope if no instance exists. As such, the bean is availablefor all pages in the application. A page author can then access the bean properties from the component tagson the page using the unified EL, as shown here: <h:outputText value="#{UserNumberBean.minimum}"/>The part of the expression before the. matches the name defined bythemanaged-bean-name element. The part of the expression after the. matches thename defined by theproperty-name element corresponding to the samemanaged-bean declaration. Notice that the application configuration resource file does not configure theuserNumber property.Any property that does not have a correspondingmanaged-property element will be initialized towhatever the constructor of the bean class has the instance variable set to.The next section explains more about using the unified EL to reference backingbeans. For more information on configuring beans using the managed bean creation Facility, seeConfiguring Beans. Using the Unified EL to Reference Backing BeansTo bind UI component values and objects to backing bean properties or toreference backing bean methods from UI component tags, page authors use the unified expressionlanguage (EL) syntax defined by JSP 2.1. As explained inUnified Expression Language, someof the features this language offers are:
These features are all especially important for supporting the sophisticated UI component modeloffered by JavaServer Faces technology. Deferred evaluation of expressions is important because the JavaServer Faces life cycle issplit into separate phases so that component event handling, data conversion and validation,and data propagation to external objects are all performed in an orderly fashion.The implementation must be able to delay the evaluation of expressions until theproper phase of the life cycle has been reached. Therefore, its tag attributesalways use deferred evaluation syntax, which is distinguished by the#{} delimiters.The Life Cycle of a JavaServer Faces Pagedescribes the life cycle in detail. In order to store data in external objects, almost all JavaServer Faces tagattributes use lvalue value expressions, which are expressions that allow both getting andsetting data on external objects. Finally, some component tag attributes accept method expressions that reference methods that handlecomponent events, or validate or convert component data. To illustrate a JavaServer Faces tag using the unified EL, let’s suppose thattheuserNo tag of theguessNumber application referenced a method rather than usingLongRangeValidator to perform the validation of user input : <h:inputText value="#{UserNumberBean.userNumber}" validator="#{UserNumberBean.validate}" />This tag binds theuserNo component’s value to theUserNumberBean.userNumber backing bean property usingan lvalue expression. It uses a method expression to refer to theUserNumberBean.validate method, which performs validation of the component’s local value. The local valueis whatever the user enters into the field corresponding to this tag. Thismethod is invoked when the expression is evaluated, which is during the processvalidation phase of the life cycle. Nearly all JavaServer Faces tag attributes accept value expressions. In addition to referencing beanproperties, value expressions can also reference lists, maps, arrays, implicit objects, and resourcebundles. Another use of value expressions is binding a component instance to a backingbean property. A page author does this by referencing the property from thebinding attribute: <inputText binding="#{UserNumberBean.userNoComponent}" />Those component tags that use method expressions areUIInput component tags andUICommandcomponent tags. See sectionsUsing Text Components andUsing Command Components for Performing Actions and Navigation for more information on how thesecomponent tags use method expressions. In addition to using expressions with the standard component tags, you can alsoconfigure your custom component properties to accept expressions by creatingValueExpression orMethodExpressioninstances for them. SeeCreating Custom Component Classes andEnabling Component Properties to Accept Expressions for more information on enabling yourcomponent’s attributes to support expressions. To learn more about using expressions to bind to backing bean properties, seeBinding Component Values and Instances to External Data Sources. For information on referencing backing bean methods from component tags, seeReferencing a Backing Bean Method. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |