|
Download
FAQ History |
|
API
Search Feedback |
Creating Model Objects
Previous releases of JavaServer Faces technology required the page author to create a model object by declaring it from the page using the
jsp:useBeantag. This technique had its disadvantages, one of which was that if a user accessed the pages of an application out of order, the bean might not have been created before a particular page was referring to it.The new way to create model objects and store them in scope is with the Managed Bean Creation facility. This facility is configured in the application configuration resource file (see section Application Configuration) using managed-bean XML elements to define each bean. This file is processed at application startup time, which means that the objects declared in it are available to the entire application before any of the pages are accessed.
The Managed Bean Creation facility has many advantages over the
jsp:useBeantag, including:
- You can create model objects in one centralized file that is available to the entire application, rather than conditionally instantiating model objects throughout the application.
- You can make changes to the model object without any additional code
- When a managed bean is created, you can customize the bean's property values directly from within the configuration file.
- Using
value-refelements, you can set the property of one managed bean to be the result of evaluating another value reference expression.- Managed beans can be created programmatically as well as from a JSP page. You'd do this by creating a
ValueBindingfor the value reference expression and then callinggetValueon it.This section shows you how to initialize model objects using the
Managed Bean Creation Facility. The section Writing a Model Object Class explains how to write a model object class. The section Binding a Component to a Bean Property explains how to reference a managed bean from the component tags.Using the managed-bean Element
You create a model object using a
managed-beanelement. Themanaged-beanelement represents an instance of a bean class that must exist in the application. At runtime, the JavaServer Faces implementation processes themanaged-beanelement and instantiates the bean as specified by the element configuration.Most of the model objects used with
cardemoare still created withjsp:useBean. TheStorefront.jsppage uses theuseBeantag to declare theCurrentOptionServermodel object:<jsp:useBean id="CurrentOptionServer" class="cardemo.CurrentOptionServer" scope="session" <jsp:setProperty name="CurrentOptionServer" property="carImage" value="current.gif"/> </jsp:useBean>To instantiate this bean using the Managed Bean Facility, you would add this
managed-beanelement configuration to the application configuration file:<managed-bean> <managed-bean-name> CurrentOptionServer </managed-bean-name> <managed-bean-class> cardemo.CurrentOptionServer </managed-bean-class> <managed-bean-scope> session </managed-bean-scope> <managed-property> <property-name>carImage</property-name> <value>current.gif</value> </managed-property> </managed-bean>The
managed-bean-nameelement defines the key under which the bean will be stored in a scope. For a component to map to this bean, the component tag'svalueRefmust match themanaged-bean-nameup to the first period. For example, consider thisvalueRefexpression that maps to thecarImageproperty of theCurrentOptionServerbean:The part before the "." matches the
managed-bean-nameofCurrentOptionServer. The section Using the HTML Tags has more examples of usingvalueRefto bind components to bean properties.The
managed-bean-classelement defines the fully-qualified name of the JavaBeans component class used to instantiate the bean. It is the application developer's responsibility to ensure that the class complies with the configuration of the bean in the application configuration resources file. For example, the property definitions must match those configured for the bean.The
managed-bean-scopeelement defines the scope in which the bean will be stored. The four acceptable scopes are: none, request, session or application. If you define the bean with a none scope, the bean is instantiated anew each time it is referenced, and so it does not get saved in any scope. One reason to use a scope of none is when a managed bean references anothermanaged-bean. The second bean should be in none scope if it is only supposed to be created when it is referenced. See Initializing Managed Bean Properties for an example of initializing a managed-bean property.The
managed-beanelement can contain zero or moremanaged-propertyelements, each corresponding to a property defined in the bean class. These elements are used to initialize the values of the bean properties. To map to a property defined by amanaged-propertyelement, the part of a component tag'svalueRefexpression after the "." must match themanaged-propertyelement'sproperty-nameelement. In the example above, thecarImageproperty is initialized with the valuecurrent.gif. The next section explains in more detail how to use themanaged-propertyelement.Initializing Properties using the managed-property Element
A
managed-propertyelement must contain aproperty-nameelement, which must match the name of the corresponding property in the bean. Amanaged-propertyelement must also contain one of a set of elements (listed in Table 21-4 on page 817) that defines the value of the property. This value must be of the same type as that defined for the property in the corresponding bean. Which element you use to define the value depends on the type of the property defined in the bean. Table 21-4 on page 817 lists all of the elements used to initialize a value.
The section Using the managed-bean Element includes an example of initializing
Stringproperties using thevaluesubelement. You also use thevaluesubelementtoinitialize primitive and other reference types. The rest of this section describes how to use thevaluesubelement and other subelements to initialize properties of typejava.util.Map,arrayandCollection, and initialization parameters.Referencing an Initialization Parameter
Another powerful feature of the Managed Bean Facility is the ability to reference implicit objects from a managed bean property.
Suppose that you have a page that accepts data from a customer, including the customer's address. Suppose also that most of your customers live in a particular zip code. You can make the zip code component render with this zip code by saving it in an implicit object and referencing it when the page is rendered.
You can save the zip code as an initial default value in the context
initParamimplicit object by setting thecontext-paramelement in yourweb.xmlfile:<context-param> <param-name>defaultZipCode</param-name> <param-value>94018</param-name> </context-param>Next, you write a
managed-beandeclaration with a property that references the parameter:<managed-bean> <managed-bean-name>customer</managed-bean-name> <managed-bean-class>CustomerBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>zipCode</property-name> <value-ref>initParam.defaultZipCode</value-ref> </managed-property> ... </managed-bean>To access the zip code at the time the page is rendered, refer to the property from the
zipcomponent tag'svalueRefattribute:Retrieving values from other implicit objects are done in a similar way. See Table 21-7 on page 825 for a list of implicit objects.
Initializing Map Properties
The
map-entrieselement is used to initialize the values of a bean property with a type ofjava.util.Map. Here is the definition ofmap-entriesfrom theweb-facesconfig_1_0.dtd(located in the<JWSDP_HOME>/jsf/libdirectory) that defines the application configuration file:As this definition shows, a
map-entrieselement contains an optionalkey-classelement, an optionalvalue-classelement and zero or moremap-entryelements.Here is the definition of
map-entryfrom the DTD:According to this definition, each of the
map-entryelements must contain akeyelement and either anull-value,value, orvalue-refelement. Here is an example that uses themap-entrieselement:<managed-bean> ... <managed-property> <property-name>cars</property-name> <map-entries> <map-entry> <key>Jalopy</key> <value>50000.00</value> </map-entry> <map-entry> <key>Roadster</key> <value-ref> sportsCars.roadster </value-ref> </map-entry> </map-entries> </managed-property> </managed-bean>The map that is created from this map-entries tag contains two entries. By default, the keys and values are all converted to
java.lang.String. If you want to specify a different type for the keys in the map, embed thekey-classelement just inside themap-entrieselement:This declaration will convert all of the keys into
java.math.BigDecimal. Of course, you need to make sure that the keys can be converted to the type that you specify. The key from the example in this section cannot be converted to ajava.math.BigDecimalbecause it is aString.If you also want to specify a different type for all of the values in the map, include the
value-classelement after thekey-classelement:<map-entries> <key-class>int</key-class> <value-class>java.math.BigDecimal</value-class> ... </map-entries>Note that this tag only sets the type of all the
valuesubelements.The first
map-entryin the example above includes avaluesubelement. Thevaluesubelement defines a single value, which will be converted to the type specified in the bean.The second
map-entrydefines avalue-refelement, which references a property on another bean. Referencing another bean from within a bean property is useful for building a system out of fine-grained objects. For example, a request-scoped form-handling object might have a pointer to an application-scoped database mapping object. Together the two can perform a form handling task. Note that including a reference to another bean will initialize the bean if it does not exist already.Instead of using a
map-entrieselement, it is also possible to assign the entire map with avalue-refelement that specifies a map-typed expression.Initializing Array and Collection Properties
The
valueselement is used to initialize the values of anarrayorCollectionproperty. Each individual value of the array orCollectionis initialized using avalue,null-value, orvalue-refelement. Here is an example:<managed-bean> ... <managed-property> <property-name>cars</property-name> <values> <value-type>java.lang.Integer</value-type> <value>Jalopy</value> <value-ref>myCarsBean.luxuryCar</value-ref> <null-value/> </values> </managed-property> </managed-bean>This example initializes an
arrayor aCollection. The type of the corresponding property in the bean determines which data structure is created. Thevalueselement defines the list of values in thearrayorCollection. Thevalueelement specifies a single value in thearrayorCollection. Thevalue-refelement references a property in another bean. Thenull-valueelement will cause the property's set method to be called with an argument ofnull. Anullproperty cannot be specified for a property whose data type is a Java primitive, such asint, orboolean.Initializing Managed Bean Properties
Sometimes you might want to create a bean that also references other managed beans so that you can construct a graph or a tree of beans. For example, suppose that you want to create a bean representing a customer's information, including the mailing address and street address, each of which are also beans. The following
managed-beandeclarations create aCustomerBeaninstance that has twoAddressBeanproperties, one representing the mailing address and the other representing the street address. This declaration results in a tree of beans withCustomerBeanas its root and the twoAddressBeanobjects as children.<managed-bean> <managed-bean-name>customer</managed-bean-name> <managed-bean-class> com.mycompany.mybeans.CustomerBean </managed-bean-class> <managed-bean-scope> request </managed-bean-scope> <managed-property> <property-name>mailingAddress</property-name> <value-ref>addressBean</value-ref> </managed-property> <managed-property> <property-name>streetAddress</property-name> <value-ref>addressBean</value-ref> </managed-property> <managed-property> <property-name>customerType</property-name> <value>New</value> </managed-property> </managed-bean> <managed-bean> <managed-bean-name>addressBean</managed-bean-name> <managed-bean-class> com.mycompany.mybeans.AddressBean </managed-bean-class> <managed-bean-scope> none </managed-bean-scope> <managed-property> <property-name>street</property-name> </null-value> <managed-property> ... </managed-bean>The first
CustomerBeandeclaration (with themanaged-bean-nameof customer) creates aCustomerBeanin request scope. This bean has two properties, calledmailingAddressandstreetAddress. These properties use thevalue-refelement to reference a bean, namedaddressBean.The second managed bean declaration defines an
AddressBean, but does not create it because itsmanaged-bean-scopeelement defines a scope of none. Recall that a scope of none means that the bean is only created when something else references it. Since both themailingAddressandstreetAddressproperties both referenceaddressBeanusing thevalue-refelement, two instances ofAddressBeanare created whenCustomerBeanis created.When you create an object that points to other objects, do not try to point to an object with a shorter life span because it might be impossible to recover that scope's resources when it goes away. A session-scoped object, for example, cannot point to a request-scoped object. And objects with "none" scope have no effective life span managed by the framework, so they can only point to other "none" scoped objects. Table 21-5 outlines all of the allowed connections:
Cycles are not permitted in forming these connections to avoid issues involving order of initialization.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.