Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
Writing a Model Object Class
A model object is a JavaBeans component that encapsulates the data on a set of UI components. It might also perform the application-specific functionality associated with the component data. For example, a model object might perform a currency conversion using a value that the user enters into a
UIInput
component and then output the conversion to aUIOutput
component. The model object follows JavaBeans component conventions in that it must contain an empty constructor and a set of properties for setting and getting the data:... String myBeanProperty = null; ... public MyBean() {} String getMyBeanProperty{ return myBeanProperty; } void setMyBeanProperty(String beanProperty){ myBeanProperty = beanProperty; }You can bind most of the component classes to model object properties, but you are not required to do so.
In order to bind a component to a model object property, the type of the property must match the type of the component object to which it is bound. In other words, if a model object property is bound to a
UISelectBoolean
component, the property should accept and return aboolean
value. The rest of this section explains how to write properties that can be bound to the component objects described in Using the HTML Tags.Writing Model Object Properties
Table 21-14 lists all the component classes described in Using the HTML Tags and the acceptable types of their values.
Make sure to use the
valueRef
attribute in the tags of the components that are mapped to model object properties. Also, be sure to use the proper names of the properties. For example, if a valueRef tag has a value ofCurrentOptionServer.currentOption
, the corresponding String property should be:String currentOption = null; String getCurrentOption(){...} void setCurrentOption(String option){...}For more information on JavaBeans conventions, see JavaBeans Components (page 672).
UIInput and UIOutput Properties
Properties for
UIInput
andUIOutput
objects accept the same types and are the most flexible in terms of the number of types they accept, as shown in Table 21-14.Most of the
UIInput
andUIOutput
properties in thecardemo
application are of typeString
. Thezip UIInput
component is mapped to anint
property inCustomerBean.java
because thezip
component is rendered with theNumber
renderer:<h:input_number id="zip" formatPattern="#####" valueRef="CustomerBean.zip" size="5"> ... </h:input_number>Here is the property mapped to the
zip
component tag:int zip = 0;
...public void setZip(int zipCode) { zip = zipCode; } public int getZip() { return zip; }
The components represented by the
input_text
,output_text
,input_hidden
, andinput_secret
tags can also be bound to theDate
,Number
andcustom types in addition to
java.lang.String
when aConverter
is applied to the component. See Performing Data Conversions for more information.UIPanel Properties
Only
UIPanel
components rendered with aData
renderer can be mapped to a model object. TheseUIPanel
components must be mapped to a JavaBeans component of typearray
,java.util.Collection
,java.util.Iterator
, orjava.util.Map
. Here is a bean that maps to thepanel_data
component from the section Using the panel_list Tag:public class CustomerListBean extends java.util.ArrayList{ public ListBean() { add(new CustomerBean("123456", "Sun Microsystems, Inc.", "SUNW", 2345.60)); add(new CustomerBean("789101", "ABC Company, Inc.", "ABC", 458.21)); } }UISelectBoolean Properties
Properties that hold this component's data must be of
boolean
type. Here is the property for thesunRoof UISelectBoolean
component:protected boolean sunRoof = false; ... public void setSunRoof(boolean roof) { sunRoof = roof; } public boolean getSunRoof() { return sunRoof; }
UISelectMany Properties
Since a
UISelectMany
component allows a user to select one or more items from a list of items, this component must map to a model object property of typejava.util.Collection
orarray
. This model object property represents the set of currently selected items from the list of available items.Here is the model object property that maps to the
valueRef
of theselectmany_checkboxlist
example from the section Using the selectmany_checkboxlist Tag:protected ArrayList currentOptions = null; public Object[] getCurrentOptions() { return currentOptions.toArray(); } public void setCurrentOptions(Object []newCurrentOptions) { int len = 0; if (null == newCurrentOptions || (len = newCurrentOptions.length) == 0) { return; } currentOptions.clear(); currentOptions = new ArrayList(len); for (int i = 0; i < len; i++) { currentOptions.add(newCurrentOptions[i]); } }Note that the
setCurrentOptions(Object)
method must clear theCollection
and rebuild it with the new set of values that the user selected.As explained in the section The UISelectMany Component, the
UISelectItem
andUISelectItems
components are used to represent all the values in aUISelectMany
component. See UISelectItem Properties and UISelectItems Properties for information on how to write the model object properties for theUISelectItem
andUISelectItems
components.UISelectOne Properties
The
UISelectOne
properties accept the same types asUIInput
andUIOutput
properties. This is because aUISelectOne
component represents the single selected item from a set of items. This item could be aString
,int
,long
, ordouble
. Here is the property corresponding to theengineOption
UISelectOne
component frommore.jsp
:protected Object currentEngineOption = engines[0]; ... public void setCurrentEngineOption(Object eng) { currentEngineOption = eng; } public Object getCurrentEngineOption() { return currentEngineOption; }Note that
currentEngineOption
is one of the objects in an array of objects, representing the list of items in theUISelectOne
component.As explained in the section The UISelectOne Component, the
UISelectItem
andUISelectItems
components are used to represent all the values in aUISelectOne
component. See UISelectItem Properties and UISelectItems Properties for information on how to write the model object properties for theUISelectItem
andUISelectItems
components.UISelectItem Properties
A
UISelectItem
component represents one value in a set of values in aUISelectMany
orUISelectOne
component. AUISelectItem
property must be mapped to a property of typeSelectItem
. ASelectItem
object is composed of: anObject
representing the value, and twoStrings
representing the label and description of theSelectItem
.Here is an example model object property for a
SelectItem
component:SelectItem itemOne = null; SelectItem getItemOne(){ return SelectItem(String value, String label, String description); } void setItemOne(SelectItem item) { itemOne = item; }UISelectItems Properties
The
UISelectItems
properties are the most difficult to write and require the most code. TheUISelectItems
components are used as children ofUISelectMany
andUISelectOne
components. EachUISelectItems
component is composed of a set ofSelectItem
instances. In your model object, you must define a set ofSelectItem
objects, set their values, and populate theUISelectItems
object with theSelectItem
objects. The following code snippet fromCurrentOptionServer
shows how to create theengineOption
UISelectItems
property.import javax.faces.component.SelectItem; ... protected ArrayList engineOption; ... public CurrentOptionServer() { protected String engines[] = { "V4", "V6", "V8" }; engineOption = new ArrayList(engines.length); ... for (i = 0; i < engines.length; i++) { engineOption.add(new SelectItem(engines[i], engines[i], engines[i])); } } ... public void setEngineOption(Collection eng) { engineOption = new ArrayList(eng); } public Collection getEngineOption() { return engineOption; }The code first initializes
engineOption
as anArrayList
. Thefor
loop creates a set ofSelectItem
objects with values, labels and descriptions for each of the engine types. Finally, the code includes the obligatorysetEngineOption
andgetEngineOption
accessor methods.
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.