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:useBean
tag. 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, page 807) 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:useBean
tag, 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-ref
elements, 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
ValueBinding
for the value reference expression and then callinggetValue
on 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-bean
element, which represents an instance of a bean class that must exist in the application. At runtime, the JavaServer Faces implementation processes themanaged-bean
element and instantiates the bean as specified by the element configuration if no instance already exists.Most of the model objects used with
cardemo
are still created withjsp:useBean
. TheStorefront.jsp
page uses theuseBean
tag to declare theCurrentOptionServer
model 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-bean
element 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-name
element defines the key under which the bean will be stored in a scope. For a component to map to this bean, the component tag'svalueRef
must match themanaged-bean-name
up to the first period. For example, consider thisvalueRef
expression that maps to thecarImage
property of theCurrentOptionServer
bean:The part before the "." matches the
managed-bean-name
ofCurrentOptionServer
. The section Using the HTML Tags has more examples of usingvalueRef
to bind components to bean properties.The
managed-bean-class
element 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-scope
element 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-bean
element can contain zero or moremanaged-property
elements, each corresponding to a property defined in the bean class. These elements are used to initialize the values of the bean properties. If you don't want a particular property initialized with a value when the bean is instantiated, do not include amanaged-property
definition for it in your application configuration file.To map to a property defined by a
managed-property
element, the part of a component tag'svalueRef
expression after the "." must match themanaged-property
element'sproperty-name
element. In the example above, thecarImage
property is initialized with the valuecurrent.gif
. The next section explains in more detail how to use themanaged-property
element.Initializing Properties using the managed-property Element
A
managed-property
element must contain aproperty-name
element, which must match the name of the corresponding property in the bean. Amanaged-property
element must also contain one of a set of elements (listed in Table 21-4 on page 823) 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 823 lists all of the elements used to initialize a value.
The section Using the managed-bean Element includes an example of initializing
String
properties using thevalue
subelement. You also use thevalue
subelementto
initialize primitive and other reference types. The rest of this section describes how to use thevalue
subelement and other subelements to initialize properties of typejava.util.Map
,array
andCollection
, 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
initParam
implicit object by setting thecontext-param
element in yourweb.xml
file:<context-param> <param-name>defaultZipCode</param-name> <param-value>94018</param-name> </context-param>Next, you write a
managed-bean
declaration 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
zip
component tag'svalueRef
attribute:Retrieving values from other implicit objects are done in a similar way. See Table 21-7 on page 831 for a list of implicit objects.
Initializing Map Properties
The
map-entries
element is used to initialize the values of a bean property with a type ofjava.util.Map
. Here is the definition ofmap-entries
from theweb-facesconfig_1_0.dtd
(located in the<
JWSDP_HOME
>/jsf/lib
directory) that defines the application configuration file:As this definition shows, a
map-entries
element contains an optionalkey-class
element, an optionalvalue-class
element and zero or moremap-entry
elements.Here is the definition of
map-entry
from the DTD:According to this definition, each of the
map-entry
elements must contain akey
element and either anull-value
,value
, orvalue-ref
element. Here is an example that uses themap-entries
element:<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 tojava.lang.String
. If you want to specify a different type for the keys in the map, embed thekey-class
element just inside themap-entries
element: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.BigDecimal
because it is aString
.If you also want to specify a different type for all of the values in the map, include the
value-class
element after thekey-class
element:<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
value
subelements.The first
map-entry
in the example above includes avalue
subelement. Thevalue
subelement defines a single value, which will be converted to the type specified in the bean.The second
map-entry
defines avalue-ref
element, 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-entries
element, it is also possible to assign the entire map with avalue-ref
element that specifies a map-typed expression.Initializing Array and Collection Properties
The
values
element is used to initialize the values of anarray
orCollection
property. Each individual value of the array orCollection
is initialized using avalue
,null-value
, orvalue-ref
element. 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
array
or aCollection
. The type of the corresponding property in the bean determines which data structure is created. Thevalues
element defines the list of values in thearray
orCollection
. Thevalue
element specifies a single value in thearray
orCollection
. Thevalue-ref
element references a property in another bean. Thenull-value
element will cause the property's set method to be called with an argument ofnull
. Anull
property 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-bean
declarations create aCustomerBean
instance that has twoAddressBean
properties, one representing the mailing address and the other representing the street address. This declaration results in a tree of beans withCustomerBean
as its root and the twoAddressBean
objects 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
CustomerBean
declaration (with themanaged-bean-name
of customer) creates aCustomerBean
in request scope. This bean has two properties, calledmailingAddress
andstreetAddress
. These properties use thevalue-ref
element to reference a bean, namedaddressBean
.The second managed bean declaration defines an
AddressBean
, but does not create it because itsmanaged-bean-scope
element 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 themailingAddress
andstreetAddress
properties both referenceaddressBean
using thevalue-ref
element, two instances ofAddressBean
are created whenCustomerBean
is 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.