|
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
UIInputcomponent and then output the conversion to aUIOutputcomponent. 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
UISelectBooleancomponent, the property should accept and return abooleanvalue. The rest of this section explains how to write properties that can be bound to the component classes 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
valueRefattribute 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.
UIInput and UIOutput Properties
Properties for
UIInputandUIOutputobjects 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
UIInputandUIOutputproperties in thecardemoapplication are of typeString. Thezip UIInputcomponent is mapped to anintproperty inCustomerBean.javabecause thezipcomponent is rendered with theNumberrenderer:<h:input_number id="zip" formatPattern="#####" valueRef="CustomerBean.zip" size="5"> ... </h:input_number>Here is the property mapped to the
zipcomponent 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_secrettags can also be bound to theDate,Numberandcustom types in addition tojava.lang.Stringwhen aConverteris applied to the component. See Performing Data Conversions for more information.UIPanel Properties
Only
UIPanelcomponents rendered with aDatarenderer can be mapped to a model object. TheseUIPanelcomponents 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_datacomponent 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
booleantype. Here is the property for thesunRoof UISelectBooleancomponent:protected boolean sunRoof = false; ... public void setSunRoof(boolean roof) { sunRoof = roof; } public boolean getSunRoof() { return sunRoof; }UISelectMany Properties
Since a
UISelectManycomponent 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.Collectionorarray. 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
valueRefof theselectmany_checkboxlistexample 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 theCollectionand rebuild it with the new set of values that the user selected.As explained in the section The UISelectMany Component, the
UISelectItemandUISelectItemscomponents are used to represent all the values in aUISelectManycomponent. See UISelectItem Properties and UISelectItems Properties for information on how to write the model object properties for theUISelectItemandUISelectItemscomponents.UISelectOne Properties
The
UISelectOneproperties accept the same types asUIInputandUIOutputproperties. This is because aUISelectOnecomponent represents the single selected item from a set of items. This item could be aString,int,long, ordouble. Here is the property corresponding to theengineOptionUISelectOnecomponent frommore.jsp:protected Object currentEngineOption = engines[0]; ... public void setCurrentEngineOption(Object eng) { currentEngineOption = eng; } public Object getCurrentEngineOption() { return currentEngineOption; }Note that
currentEngineOptionis one of the objects in an array of objects, representing the list of items in theUISelectOnecomponent.As explained in the section The UISelectOne Component, the
UISelectItemandUISelectItemscomponents are used to represent all the values in aUISelectOnecomponent. See UISelectItem Properties and UISelectItems Properties for information on how to write the model object properties for theUISelectItemandUISelectItemscomponents.UISelectItem Properties
A
UISelectItemcomponent represents one value in a set of values in aUISelectManyorUISelectOnecomponent. AUISelectItemproperty must be mapped to a property of typeSelectItem. ASelectItemobject is composed of: anObjectrepresenting the value, and twoStringsrepresenting the label and description of theSelectItem.Here is an example model object property for a
SelectItemcomponent:SelectItem itemOne = null; SelectItem getItemOne(){ return SelectItem(String value, String label, String description); } void setItemOne(SelectItem item) { itemOne = item; }UISelectItems Properties
The
UISelectItemsproperties are the most difficult to write and require the most code. TheUISelectItemscomponents are used as children ofUISelectManyandUISelectOnecomponents. EachUISelectItemscomponent is composed of a set ofSelectIteminstances. In your model object, you must define a set ofSelectItemobjects, set their values, and populate theUISelectItemsobject with theSelectItemobjects. The following code snippet fromCurrentOptionServershows how to create theengineOptionUISelectItemsproperty.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
engineOptionas anArrayList. Theforloop creates a set ofSelectItemobjects with values, labels and descriptions for each of the engine types. Finally, the code includes the obligatorysetEngineOptionandgetEngineOptionaccessor 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.