|
Download
FAQ History |
|
API
Search Feedback |
Using the JavaServer Faces Tag Libraries
JavaServer Faces technology provides two tag libraries: the
html_basictag library and thejsf-coretag library. Thehtml_basictag library defines tags for representing common HTML user interface components. Thejsf-coretag library defines all of the other tags, including tags for registering listeners and validators on components. The tags injsf-coreare independent of any rendering technology and can therefore be used with any render kit. Using these tag libraries is similar to using any other custom tag library. This section assumes that you are familiar with the basics of Custom Tags in JSP Pages.Declaring the JavaServer Faces Tag Libraries
To use the JavaServer Faces tag libraries, you need to include these taglib directives at the top of each page containing the tags defined by these tag libraries:
<%@ taglib uri="http://java.sun.com/jsf/html/" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core/" prefix="f" %>The
uriattribute value uniquely identifies the tag library. Theprefixattribute value is used to distinguish tags belonging to the tag library. For example, theformtag must be referenced in the page with thehprefix, like this:When you reference any of the JavaServer Faces tags from within a JSP page, you must enclose them in the
use_facestag, which is defined in thejsf_corelibrary:You can enclose other content within the
use_facestag, including HTML and other JSP tags, but all JavaServer Faces tags must be enclosed within theuse_facestag.Using the Core Tags
The tags defined by the
jsf-coreTLD represent a set of tags for performing core actions that are independent of a particular render kit. The jsf-core tags are listed in Table 21-8.
These tags are used in conjunction with component tags and are therefore explained in other sections of this tutorial. Table 21-9 lists which sections explain how to use which
jsf-coretags.
Table 21-9 Where the jsf-core Tags are Explained Tags Where Explained Event-Handling TagsattributeTagfacetTagparameterTag Linking to a URL, anduse_facesTag Validator Tags
Using the HTML Tags
The tags defined by
html_basicrepresent HTML form controls and other basic HTML elements. These controls display data or accept data from the user. This data is collected as part of a form and is submitted to the server, usually when the user clicks a button. This section explains how to use each of the component tags shown in Table 20-2, and is organized according to theUIComponentclasses from which the tags are derived.This section does not explain every tag attribute, only the most commonly-used ones. Please refer to
html_basic.tldfile in thelibdirectory of your download for a complete list of tags and their attributes.In general, most of the component tags have these attributes in common:
In this release, the
idattribute is not required for a component tag except in these situations:
- Another component or a server-side class must refer to the component
- The component tag is impacted by a JSTL conditional or iterator tag (for more information, see Flow Control Tags).
If you don't include an
idattribute, the JavaServer Faces implementation automatically generates a component ID.
UIOutputand subclasses ofUIOutputhave avalueRefattribute, which is always optional, except in the case ofSelectItems. Using thevalue-refattribute to bind to a data source is explained more in section Using the Core Tags.The UIForm Component
A
UIFormcomponent is an input form with child components representing data that is either presented to the user or submitted with the form. Theformtag encloses all of the controls that display or collect data from the user. Here is theformtag from theImageMap.jsppage:The
formNameattribute is passed to the application, where it is used to select the appropriate business logic.The
formtag can also include HTML markup to layout the controls on the page. Theformtag itself does not perform any layout; its purpose is to collect data and to declare attributes that can be used by other components in the form.The UICommand Component
The
UICommandcomponent performs an action when it is activated. The most common example of such a component is the button. This release supportsButtonandHyperlinkasUICommandcomponent renderers.Using the command_button Tag
Most pages in the
cardemoexample use thecommand_buttontag. When the button is clicked, the data from the current page is processed, and the next page is opened. Here is thebuyButtoncommand_buttontag frombuy.jsp:Clicking the button will cause
Customer.jspto open. This page allows you to fill in your name and shipping information.The
keyattribute references the localized message for the button's label. Thebundleattribute references theResourceBundlethat contains a set of localized messages. For more information on localizing JavaServer Faces applications, see Performing Localization.The
commandNameattribute refers to the name of the command generated by the event of clicking the button. ThecommandNameis used by theActionEventListenerto determine how to process the command. See Handling Events for more information on how to implement event listeners to process the event generated by button components.The
actionattribute represents a literal outcome value returned when the button is clicked. The outcome is passed to the defaultNavigationHandler, which matches the outcome against a set of navigation rules defined in the application configuration file.A
command_buttontag can have anactionRefattribute as an alternative to theactionattribute. TheactionRefattribute is a value reference expression that points to anAction, whose invoke method performs some processing and returns the logical outcome.See section Navigating Between Pages for information on how to use the
actionandactionRefattributes.The
cardemoapplication uses thecommandNameand theactionattributes together. This is because it uses the outcome from theactionattribute to navigate between pages, but it also uses thecommandNameattribute to point to a listener that performs some other processing. In practice, this extra processing should be performed by theActionobject, and theactionRefattribute should be used to point to theActionobject. ThecommandNameattribute and its associated listener should only be used to process UI changes that don't result in a page being loaded.Using the command_hyperlink Tag
The
command_hyperlinktag represents an HTML hyperlink and is rendered as an HTML<a>element. Thecommand_hyperlinktag can be used for two purposes:
- To submit
ActionEventsto the application. See Handling Events and Navigating Between Pages for more information.- To link to a particular URL
Submitting ActionEventsLike the
command_buttontag, thecommand_hyperlinktag can be used to submitActionEvents. To submit anActionEventfor the purpose of navigating between pages, the tag needs one of these attributes:The
actionattribute represents a literal outcome value returned when the hyperlink is clicked. The outcome is passed to the defaultNavigationHandler, which matches the outcome against a set of navigation rules defined in the application configuration file.The
actionRefattribute is a value reference expression that points to anAction, whoseinvokemethod performs some processing and returns the logical outcome.See section Navigating Between Pages for information on how to use the
actionandactionRefattributes.To submit an
ActionEventfor the purpose of making UI changes, the tag needs both of these attributes:The
commandNameattribute refers to the name of the command generated by the event of clicking the hyperlink. ThecommandNameis used by theActionEventListenerto determine how to process the command. See Handling Events for more information on how to implement event listeners to process the event generated by button components.The
commandNameattribute and its associated listener should only be used to process UI changes that don't result in a page being loaded. See Registering Listeners on Components for more information on using thecommandNameattribute.In addition to these attributes, the tag also needs a
labelattribute, which is the text that the user clicks to generate the event.A
command_hyperlinktag can containparametertags that will cause an HTML<input type=hidden>element to be rendered. Thisinputtag represents a hidden control that stores the name and value specified in theparametertags between client/server exchanges so that the server-side classes can retrieve the value. The following two tags showcommand_hyperlinktags that submitActionEvents. The first tag does not use parameters; the second tag does use parameters.<h:command_hyperlink id="commandParamLink" commandName="login" commandClass="LoginListener" label="link text"/> <h:command_hyperlink id="commandParamLink" commandName="login" commandClass="LoginListener" label="Login"> <f:parameter id="Param1" name="name" valueRef="LoginBean.name"/> <f:parameter id="Param2" name="value" valueRef="LoginBean.password"/> </h:command_hyperlink>The first tag renders this HTML:
<a href="#" onmousedown="document.forms[0].commandParamLink.value='login'; document.forms[0].submit()" class="hyperlinkClass"> link text</a> <input type="hidden" name="commandParamLink"/>The second tag renders this HTML, assuming that
LoginBean.nameisdukeandLoginBean.passwordisredNose:<a href="#" onmousedown="document.forms[0].commandParamLink.value='login'; document.forms[0].submit()" class="hyperlinkClass"> link text</a> <input type="hidden" name="commandParamLink"/> <input type="hidden" name="name" value="duke"/> <input type="hidden" name="value" value="redNose"/>
Note: Notice that the
command_hyperlinktag that submitsActionEventswill render JavaScript. If you use this tag, make sure your browser is JavaScript-enabled.
Linking to a URLTo use
command_hyperlinkto link to a URL, yourcommand_hyperlinktag must specify thehrefattribute, indicating the page to which to link.A
command_hyperlinkthat links to a URL can also containparametertags. Theparametertags for this kind ofcommand_hyperlinktag allow the page author to add query strings to the URL. The following two tags showcommand_hyperlinktags that link to a URL. The first tag does not use parameters; the second tag does use parameters.<h:command_hyperlink id="hrefLink" href="welcome.html" image="duke.gif"/> <h:command_hyperlink id="hrefParamLink" href="welcome.html" image="duke.gif"> <f:parameter id="Param1" name="name" valueRef="LoginBean.name"/> <f:parameter id="Param2" name="value" valueRef="LoginBean.password"/> </h:command_hyperlink>The first tag renders this HTML:
The second tag renders the following HTML, assuming that
LoginBean.nameis duke andLoginBean.passwordis redNose:The UIGraphic Component
The
UIGraphiccomponent displays an image. Thecardemoapplication has many examples ofgraphic_imagetags. Here is thegraphic_imagetag used with the image map onImageMap.jsp:The
urlattribute specifies the path to the image. It also corresponds to the local value of theUIGraphiccomponent so that the URL can be retrieved with thecurrentValuemethod or indirectly from a model object. The URL of the example tag begins with a "/", which adds the relative context path of the Web application to the beginning of the path to the image.The
usemapattribute refers to the image map defined by the customUIMapcomponent on the same page. See Creating Custom UI Components for more information on the image map.The UIInput and UIOutput Components
The
UIInputcomponent displays a value to a user and allows the user to modify this data. The most common example is a text field. TheUIOutputcomponent displays data that cannot be modified. The most common example is a label.Both
UIInputandUIOutputcomponents can be rendered in several different ways. Since the components have some common functionality, they share many of the same renderers.Table 21-10 lists the common renderers of
UIInputandUIOutput. Recall from The Component Rendering Model that the tags are composed of the component and the renderer. For example, theinput_texttag refers to aUIInputcomponent that is rendered with theTextRenderer.
In addition to the renderers listed in Table 21-10,
UIInputandUIOutputeach support other renderers that the other component does not support. These are listed in Table 21-11.
All of the tags listed in Table 21-10--except for the
input_textandoutput_texttags--display or accept data of a particular format specified in thejava.textorjava.utilpackages. You can also apply theDate,DateTime,Number, andTimerenderers associated with these tags to convert data associated with theinput_text,output_text,input_hidden, andinput_secrettags. See Performing Data Conversions for more information on using these renderers as converters.The rest of this section explains how to use selected tags listed in the two tables above. These tags are:
input_datetime,output_datetime,output_label,output_message,input_secret,output_text, andinput_text.The
output_errorstag is explained in Performing Validation. The tags associated with theDate,Number, andTimerenderers are defined in a similar way to those tags associated with theDateTimerenderer. Theinput_hiddenandinput_textareatags are similar to theinput_texttag. Refer to thehtml_basicTLD in your download to see what attributes are supported for these extra tags.Using the input_datetime and output_datetime Tags
The
DateTimerenderer can render bothUIInputandUIOutputcomponents. Theinput_datetimetag displays and accepts data in ajava.text.SimpleDateFormat. Theoutput_datetimetag displays data in ajava.text.SimpleDateFormat. This section shows you how to use theoutput_datetimetag. Theinput_datetimetag is written in a similar way.The
output_datetimeandinput_datetimetags have the following attributes and values for formatting data:See
java.text.SimpleDateFormatandjava.util.TimeZonefor information on specifying the style ofdateStyle,timeStyle, andtimezone. You can use the first three attributes in the same tag simultaneously or separately. Or, you can simply useformatPatternto specify aStringpattern to format the data. The following tag is an example of using theformatPatternattribute:One example of a date and time that this tag can display is:
You can also display the same date and time with this tag:
The application developer is responsible for ensuring that the
LoginBean.dateproperty is the proper type to handle these formats.The tags corresponding to the
Date,Number, andTimerenderers are written in a similar way. See thehtml_basicTLD in thelibdirectory of your installation to look up the attributes supported by the tags corresponding to these renderers.Using the output_text and input_text Tags
The
Textrenderer can render bothUIInputandUIOutputcomponents. Theinput_texttag displays and accepts a single-line string. Theoutput_texttag displays a single-line string. This section shows you how to use theinput_texttag. Theoutput_texttag is written in a similar way.The following attributes, supported by both
output_textandinput_text, are likely to be the most commonly used:
id: Identifies the component associated with this tagvalueRef: Identifies the model object property bound to the componentconverter: Identifies one of the renderers that will be used to convert the component's local data to the model object property data specified in thevalueRefattribute. See Performing Data Conversions for more information on how to use this attribute.value: Allows the page author to specify the local value of the component.The
output_texttag also supports thekeyandbundleattributes, which are used to fetch the localized version of the component's local value. See Performing Localization for more information on how to use these attributes.Here is an example of an
input_texttag from theCustomer.jsppage:The
valueRefvalue refers to thefirstNameproperty on theCustomerBeanmodel object. After the user submits the form, the value of thefirstNameproperty inCustomerBeanwill be set to the text entered in the field corresponding to this tag.Using the output_label Tag
The
output_labeltag is used to attach a label to a specified input field for accessibility purposes. Here is an example of anoutput_labeltag:<h:output_label for="firstName"> <h:output_text id="firstNameLabel" value="First Name"/> </h:output_label> ... <h:input_text id="firstName" />The
forattribute maps to theidof the input field to which the label is attached. Theoutput_texttag nested inside theoutput_labeltag represents the actual label. Thevalueattribute on theoutput_texttag indicates the label that is displayed next to the input field.Using the output_message Tag
The
output_messagetag allows a page author to display concatenated messages as aMessageFormatpattern. Here is an example of anoutput_messagetag:<h:output_message value="Goodbye, {0}. Thanks for ordering your {1} " > <f:parameter id="param1" valueRef="LoginBean.name"/> <f:parameter id="param2" valueRef="OrderBean.item" /> </h:output_message>The
valueattribute specifies theMessageFormatpattern. Theparametertags specify the substitution parameters for the message. ThevalueRefforparam1maps to the user's name in theLoginBean. This value replaces{0}in the message. ThevalueRefforparam2maps to the item the user ordered in theOrderBean. This value replaces{1}in the message. Make sure you put theparametertags in the proper order so that the data is inserted in the correct place in the message.Instead of using
valueRef, a page author can hardcode the data to be substituted in the message by using thevalueattribute on theparametertag.Using the input_secret Tag
The
input_secrettag renders an<input type="password">HTML tag. When the user types a string in this field, a row of asterisks is displayed instead of the string the user types. Here is an example of aninput_secrettag:In this example, the
redisplayattribute is set to false. This will prevent the password from being displayed in a query string or in the source file of the resulting HTML page.The UIPanel Component
A
UIPanelcomponent is used as a layout container for its children. When using the renderers from the HTML render kit, aUIPanelis rendered as an HTML table. Table 21-12 lists all of the renderers and tags corresponding to theUIPanelcomponent.
The
panel_gridandpanel_listtags are used to represent entire tables. Thepanel_datatags andpanel_grouptags are used to represent rows in the tables. To represent individual cells in the rows, theoutput_texttag is usually used, but any output component tag can be used to represent a cell.A
panel_datatag can only be used in apanel_list. Apanel_groupcan be used in bothpanel_gridtags andpanel_listtags. The next two sections show you how to create tables withpanel_gridandpanel_list, and how to use thepanel_dataandpanel_grouptags to generate rows for the tables.Using the panel_grid Tag
The
panel_gridtag has a set of attributes that specify CSS stylesheet classes: thecolumnClasses,footerClass,headerClass,panelClass, androwClasses. These stylesheet attributes are not required.The
panel_gridtag also has acolumnsattribute. Thecolumnsattribute is required if you want your table to have more than one column because thecolumnsattribute tells the renderer how to group the data in the table.If a
headerClassis specified, thepanel_gridmust have a header as its first child. Similarly, if afooterClassis specified, thepanel_gridmust have a footer as its last child.The
cardemoapplication includes onepanel_gridtag on thebuy.jsppage:<h:panel_grid id="choicesPanel" columns="2" footerClass="subtitle" headerClass="subtitlebig" panelClass="medium" columnClasses="subtitle,medium"> <f:facet name="header"> <h:panel_group> <h:output_text key="buyTitle" bundle="carDemoBundle"/> </h:panel_group> </f:facet> <h:output_text key="Engine" bundle="carDemoBundle" /> <h:output_text valueRef= "CurrentOptionServer.currentEngineOption"/> ... <h:output_text key="gpsLabel" bundle="carDemoBundle" /> <h:output_text valueRef="CurrentOptionServer.gps" /> <f:facet name="footer"> <h:panel_group> <h:output_text key="yourPriceLabel" bundle="carDemoBundle" /> <h:output_text valueRef="CurrentOptionServer.packagePrice" /> </h:panel_group> </f:facet> </h:panel_grid>This
panel_gridis rendered to a table that lists all of the options that the user chose on the previous page,more.jsp. Thispanel_griduses stylesheet classes to format the table. The CSS classes are defined in thestylesheet.cssfile in the <JWSDP_HOME>/jsf/samples/cardemo/webdirectory of your installation. Thesubtitlebigdefinition is:.subtitlebig { font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #93B629; padding-top: 10; padding-bottom: 10; }Since the
panel_gridtag specifies aheaderClassand afooterClass, thepanel_gridmust contain a header and footer. Usually, afacettag is used to represent headers and footers. This is because header and footer data is usually static.A facet is used to represent a component that is independent of the parent-child relationship of the page's component tree. Since header and footer data is static, the elements representing headers and footers should not be updated like the rest of the components in the tree.
This
panel_griduses afacettag for both the headers and footers. Facets can only have one child, and so apanel_grouptag is needed to group more than one element within afacet. In the case of theheaderfacet, apanel_grouptag is not really needed. This tag could be written like this:The
panel_grouptag is needed within thefooterfacet tag because the footer requires two cells of data, represented by the twooutput_texttags within thepanel_grouptag:<f:facet name="footer"> <h:panel_group> <h:output_text key="yourPriceLabel" bundle="carDemoBundle" /> <h:output_text valueRef="CurrentOptionServer.packagePrice" /> </h:panel_group> </f:facet>A
panel_grouptag can also be used to encapsulate a nested tree of components so that the parent thinks of it as a single component.In between the
headerandfooterfacet tags, are theoutput_texttags, each of which represents a cell of data in the table:<h:output_text key="Engine" bundle="carDemoBundle" /> <h:output_text valueRef= "CurrentOptionServer.currentEngineOption"/> ... <h:output_text key="gpsLabel" bundle="carDemoBundle" /> <h:output_text valueRef="CurrentOptionServer.gps" />Again, the data represented by the
output_texttags is grouped into rows according to the value of the columns attribute of theoutput_texttag. Thecolumnsattribute in the example is set to "2". So from the list ofoutput_texttags representing the table data, the data from the oddoutput_texttags is rendered in the first column and the data from the evenoutput_texttags is rendered in the second column.Using the panel_list Tag
The
panel_listtag has the same set of stylesheet attributes aspanel_grid, but it does not have acolumnsattribute. The number of columns in the table equals the number ofoutput_text(or other component tag) elements within thepanel_datatag, which is nested inside thepanel_listtag. Thepanel_datatag iterates over aCollection,array,Iterator, orMapof model objects. Eachoutput_texttag nested in apanel_datatag maps to a particular property of each of the model objects in the list. Here is an example of apanel_listtag:<h:panel_list id="Accounts" > <f:facet name="header"> <h:panel_group> <h:output_text id="acctHead" value="Account Id"/> <h:output_text id="nameHead" value="Customer Name"/> <h:output_text id="symbolHead" value="Symbol"/> <h:output_text id="tlSlsHead" value="Total Sales"/> </h:panel_group> </f:facet> <h:panel_data id="tblData" var="customer" valueRef="CustomerListBean"> <h:output_text id="acctId" valueRef="customer.acctId"/> <h:output_text id="name" valueRef="customer.name"/> <h:output_text id="symbol" valueRef="customer.symbol"/> <h:output_text id="tlSls" valueRef="customer.totalSales"/> </h:panel_data> </h:panel_list>This example uses a
facettag, and a set ofoutput_texttags nested inside apanel_grouptag to represent a header row. See the previous section for a description of using facets andpanel_grouptags.The component represented by the
panel_datatag maps to a bean that is aCollection,array,Iterator, orMapof beans. ThevalueRefattribute refers to this bean, calledCustomerListBean. Thevarattribute refers to the current bean in theCustomerListBeanlist. In this example, the current bean in the list is calledcustomer. Each component represented by an output_text tag maps to a property on thecustomerbean.The
panel_datatag's purpose is to iterate over the model objects and allow theoutput_texttags to render the data from each bean in the list. Each iteration over the list of beans will produce one row of data.One example table that can be produced by this
panel_listtag is:
Table 21-13 Example Accounts Table Account Id Customer Name Symbol Total Sales 123456 Sun Microsystems, Inc. SUNW 2345.60 789101 ABC Company ABC 458.21
The UISelectBoolean Component
The
UISelectBooleanclass defines components that have abooleanvalue. Theselectboolean_checkboxtag is the only tag that JavaServer Faces technology provides for representing boolean state. Themore.jsppage has a set ofselectboolean_checkboxtags. Here is the one representing the cruisecontrol component:<h:selectboolean_checkbox id="cruisecontrol" title="Cruise Control" valueRef="CurrentOptionServer.cruiseControlSelected" > <f:valuechanged_listener type="cardemo.PackageValueChanged"/> </h:selectboolean_checkbox>The
idattribute value refers to the component object. Thelabelattribute value is what is displayed next to the checkbox. ThevalueRefattribute refers to the model object property associated with the component. The property that aselectboolean_checkboxtag maps to should be of typeboolean, since a checkbox represents a boolean value.The UISelectMany Component
The
UISelectManyclass defines components that allow the user to select zero or more values from a set of values. This component can be rendered as a checkboxlist, a listbox, or a menu. This section explains theselectmany_checkboxlistandselectmany_menutags. Theselectmany_listboxtag is similar to theselectmany_menutag, exceptselectmany_listboxdoes not have a size attribute since a listbox displays all items at once.Using the selectmany_checkboxlist Tag
The
selectmany_checkboxlisttag renders a set of checkboxes with each checkbox representing one value that can be selected. The cardemo does not have an example of aselectmany_checkboxlisttag, but this tag can be used to render the checkboxes on themore.jsppage:<h:selectmany_checkboxlist valueRef="CurrentOptionServer.currentOptions"> <h:selectitem itemLabel="Sunroof" valueRef="CurrentOptionServer.sunRoofSelected"> <f:valuechanged_listener type="cardemo.PackageValueChanged" /> </h:selectitem> <h:selectitem itemLabel="Cruise Control" valueRef= "CurrentOptionServer.cruiseControlSelected" > <f:valuechanged_listener type="cardemo.PackageValueChanged" /> </h:selectitem> </h:selectmany_checkboxlist>The
valueRefattribute identifies the model object property,currentOptions, for the current set of options. This property holds the values of the currently selected items from the set of checkboxes.The
selectmany_checkboxlisttag must also contain a tag or set of tags representing the set of checkboxes. To represent a set of items, you use theselectitemstag. To represent each item individually, use aselectitemtag for each item. The UISelectItem and UISelectItems Classes section explains these two tags in more detail.Using the selectmany_menu Tag
The
selectmany_menutag represents a component that contains a list of items, from which a user can choose one or more items. The menu is also commonly known as a drop-down list or a combo box. The tag representing the entire list is theselectmany_menutag. Here is an example of aselectmany_menutag:<h:selectmany_menu id="fruitOptions" valueRef="FruitOptionBean.chosenFruits"> <h:selectitems valueRef="FruitOptionBean.allFruits"/> </h:selectmany_menu>The attributes of the
selectmany_menutag are the same as those of theselectmany_checkboxlisttag. Again, thevalueRefof theselectmany_menutag maps to the property that holds the currently selected items' values. Aselectmany_menutag can also have a size attribute, whose value specifies how many items will display at one time in the menu. When the size attribute is set, the menu will render with a scrollbar for scrolling through the displayed items.Like the
selectmany_checkboxlisttag, theselectmany_menutag must contain either aselectitemstag or a set ofselectitemtags for representing the items in the list. ThevalueRefattribute of theselectitemstag in the example maps to the property that holds all of the items in the menu. The UISelectItem and UISelectItems Classes explains these two tags.The UISelectOne Component
The
UISelectOneclass defines components that allow the user to select one value from a set of values. This component can be rendered as a listbox, a radio button, or a menu. Thecardemoexample uses theselectone_radioandselectone_menutags. Theselectone_listboxtag is similar to theselectone_menutag, exceptselectone_listboxdoes not have asizeattribute since a listbox displays all items at once. This section explains how to use theselectone_radioandselectone_menutags.Using the selectone_radio Tag
The
selectone_radiotag renders a set of radio buttons, one for each value that can be selected. Here is aselectone_radiotag frommore.jspthat allows you to select a brake option:<h:selectone_radio id="currentBrake" valueRef="CurrentOptionServer.currentBrakeOption"> <f:valuechanged_listener type="cardemo.PackageValueChanged"/> <h:selectitems valueRef="CurrentOptionServer.brakeOption"/> </h:selectone_radio>The
idattribute of theselectone_radiotag uniquely identifies the radio group. The id is only required if another component, model object, or listener must refer to this component; otherwise, the JavaServer Faces implementation will generate a component id for you.The
valueRefattribute identifies the model object property forbrakeOption, which iscurrentBrakeOption. This property holds the value of the currently selected item from the set of radio buttons. ThecurrentBrakeOptionproperty can be any of the types supported by JavaServer Faces technology.The
selectone_radiotag must also contain a tag or set of tags representing the list of items contained in the radio group. To represent a set of tags, you use theselectitemstag. To represent each item individually, use aselectitemtag for each item. The UISelectItem and UISelectItems Classes explains these two tags in more detail.Using the selectone_menu Tag
The
selectone_menutag represents a component that contains a list of items, from which a user can choose one item. The menu is also commonly known as a drop-down list or a combo box. The tag representing the entire list is theselectone_menutag. Here is theselectone_menutag from themore.jsppage:<h:selectone_menu id="currentEngine" valueRef="CurrentOptionServer.currentEngineOption"> <f:valuechanged_listener type="cardemo.PackageValueChanged" /> <h:selectitems valueRef="CurrentOptionServer.engineOption"/> </h:selectone_menu>The attributes of the
selectone_menutag are the same as those of theselectone_radiotag. Again, thevalueRefof theselectone_menutag maps to the property that holds the currently selected item's value. Aselectone_menutag can also have asizeattribute, whose value specifies how many items will display at one time in the menu. When thesizeattribute is set, the menu will render with a scrollbar for scrolling through the displayed items.Like the
selectone_radiotag, theselectone_menutag must contain either aselectitemstag or a set ofselectitemtags for representing the items in the list. The UISelectItem and UISelectItems Classes section explains these two tags.The UISelectItem and UISelectItems Classes
The
UISelectItemand theUISelectItemsclasses represent components that can be nested inside aUISelectOneor aUISelectManycomponent. TheUISelectItemis associated with aSelectIteminstance, which contains the value, label, and description of a single item in theUISelectOneorUISelectManycomponent. TheUISelectItemsclass represents a set ofSelectIteminstances, containing the values, labels, and descriptions of the entire list of items.The
selectitemtag represents aUISelectItemcomponent. Theselectitemstag represents aUISelectItemscomponent. You can use either a set ofselectitemtags or a singleselectitemstag within yourselectoneorselectmanytags.The advantages of using
selectitemsareThe advantages of using
selectitemare:For more information on writing model object properties for the UI
SelectItemscomponents, see Writing Model Object Properties. The rest of this section shows you how to use theselectitemsandselectitemtags.The selectitems Tag
Here is the
selectone_menutag from the section The UISelectOne Component:<h:selectone_menu id="currentEngine" valueRef="CurrentOptionServer.currentEngineOption"> <f:valuechanged_listener type="cardemo.PackageValueChanged" /> <h:selectitems valueRef="CurrentOptionServer.engineOption"/> </h:selectone_menu>The
idattribute of theselectitemstag refers to theUISelectItemscomponent object.The
valueRefattribute binds theselectitemstag to theengineOptionproperty ofCurrentOptionServer.In the
CurrentOptionServer, theengineOptionproperty has a type ofArrayList:
UISelectItemsis a collection ofSelectIteminstances. You can see this by noting how theengineOptionArrayListis populated:for (i = 0; i < engines.length; i++) { engineOption.add(new SelectItem(engines[i], engines[i], engines[i])); }The arguments to the
SelectItemconstructor are:The section UISelectItems Properties describes in more detail how to write a model object property for a
UISelectItemscomponentThe selectitem Tag
The
cardemoapplication contains a few examples ofselectitemtags, but let's see how theengineOptiontag would look if you usedselectiteminstead ofselectitems:<h:selectone_menu id="engineOption" valueRef="CurrentOptionServer.currentEngineOption"> <h:selectitem itemValue="v4" itemLabel="v4"/> <h:selectitem itemValue="v6" itemLabel="v6"/> <h:selectitem itemValue="v8" itemLabel="v8"/> </h:selectone_menu>The
selectone_menutag is exactly the same and maps to the same property, representing the currently selected item.The
itemValueattribute represents the default value of theSelectIteminstance. TheitemLabelattribute represents theStringthat appears in the dropdown list component on the page.You can also use a
valueRefattribute instead of theitemValueattribute to refer to a bean property that represents the item's value.
|
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.