Download
FAQ History |
API
Search Feedback |
Using the HTML Component Tags
The tags defined by the JavaServer Faces standard HTML render kit tag library represent 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 17-2, and is organized according to the
UIComponent
classes from which the tags are derived.The next section explains the more important tag attributes that are common to most component tags. Please refer to the TLD documentation at
http://java.sun.com/j2ee/javaserverfaces/1.0/docs/tlddocs/index.html
for a complete list of tags and their attributes.For each of the components discussed in the following sections, Writing Component Properties explains how to write a bean property bound to a particular UI component or its value.
UI Component Tag Attributes
In general, most of the component tags support these attributes:
id
: Uniquely identifies the componentimmediate
: If set totrue
, indicates that any events, validation, and conversion associated with the component should happen in the apply request values phase rather than a later phase.rendered
: Specifies a condition in which the component should be rendered. If the condition is not satisfied, the component is not rendered.style
: Specifies a Cascading Style Sheet (CSS) style for the tag.styleClass
: Specifies a CSS stylesheet class that contains definitions of the styles.value
: Identifies an external data source and binds the component's value to it.binding
: Identifies a bean property and binds the component instance to it.All of the UI component tag attributes (except id and var) are value-binding-enabled, which means that they accept JavaServer Faces EL expressions. These expressions allow you to use mixed literals and JSP 2.0 expression language syntax and operators. See Expression Language for more information about the JSP 2.0 expression language.
The id Attribute
The
id
attribute 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
id
attribute, the JavaServer Faces implementation automatically generates a component ID.The immediate Attribute
UIInput
components and command components (those that implementActionSource
, such as buttons and hyperlinks)
can set theimmediate
attribute totrue
to force events, validations, and conversions to be processed during the apply request values phase of the life cycle. Page authors need to carefully consider how the combination of an input component'simmediate
value and a command component'simmediate
value determines what happens when the command component is activated.Assume that you have a page with a button and a field for entering the quantity of a book in a shopping cart. If both the button's and the field's
immediate
attributes are set totrue
, the new value of the field will be available for any processing associated with the event that is generated when the button is clicked. The event associated with the button and the event, validation, and conversion associated with the field are all handled during the apply request values phase.If the button's
immediate
attribute is set totrue
but the field'simmediate
attribute is set tofalse
, the event associated with the button is processed without updating the field's local value to the database. This is because any events, conversion, or validation associated with the field occurs during its usual phases of the life cycle, which come after the apply request values phase.The
bookshowcart.jsp
page of the Duke's Bookstore application has examples of components using theimmediate
attribute to control which component's data is updated when certain buttons are clicked. Thequantity
field for each book has itsimmediate
attribute set tofalse
. (Thequantity
fields are generated by theUIData
component. See The UIData Component, for more information.) Theimmediate
attribute of the Continue Shopping hyperlink is set totrue
. Theimmediate
attribute of the Update Quantities hyperlink is set tofalse
.If you click the Continue Shopping hyperlink, none of the changes entered into the quantity input fields will be processed. If you click the Update Quantities hyperlink, the values in the quantity fields will be updated in the shopping cart.
The rendered Attribute
A component tag uses a Boolean JavaServer Faces expression language (JavaServer Faces EL) expression, along with the
rendered
attribute, to determine whether or not the component will be rendered. For example, thecheck
commandLink
component on thebookcatalog.jsp
page is not rendered if the cart contains no items:<h:commandLink id="check" ... rendered="#{cart.numberOfItems > 0}"> <h:outputText value="#{bundle.CartCheck}"/> </h:commandLink>The style and styleClass Attributes
The
style
andstyleClass
attributes allow you to specify CSS styles for the rendered output of your component tags. The UIMessage and UIMessages Components describes an example of using thestyle
attribute to specify styles directly in the attribute. A component tag can instead refer to a Cascading Style Sheets (CSS) stylesheet class. ThedataTable
tag on thebookcatalog.jsp
page of the Duke's Bookstore application references the style classlist-background
:The stylesheet that defines this class is
stylesheet.css
, which is included in the application. For more information on defining styles, please see Cascading Style Sheets athttp://www.w3.org/Style/CSS/
.The value and binding Attributes
UIOutput
and subclasses ofUIOutput
havevalue
andbinding
attributes, which are always optional, except in the case ofSelectItems
, which requires avalue
attribute. Using thevalue
andbinding
attributes is explained more in Binding Component Values and Instances to External Data Sources.The UIForm Component
A
UIForm
component is an input form that has child components representing data that is either presented to the user or submitted with the form. Theform
tag encloses all the controls that display or collect data from the user. Here is an example:The
form
tag can also include HTML markup to lay out the controls on the page. Theform
tag 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. A page can include multipleform
tags, but only the values from the form that the user submits will be included in the postback.The UIColumn Component
The
UIColumn
component represents a column of data in aUIData
component. While theUIData
component is iterating over the rows of data, it processes theUIColumn
for each row.UIColumn
has no renderer associated with it and is represented on the page with acolumn
tag. Here is an examplecolumn
tag from thebookshowcart.jsp
page of the Duke's Bookstore example:<h:dataTable id="items" ... value="#{cart.items}" var="book"> ...<h:column>
<f:facet name="header"> <h:outputText value="#{bundle.ItemQuantity}"/> </f:facet> <h:inputText ... value="#{item.quantity}"> <f:validateLongRange minimum="1"/> </h:inputText>
</h:column>
... </h:dataTable>The
UIData
component in this example iterates through the list of books (cart.items
) in the shopping cart and displays their titles, authors, and prices. Thecolumn
tag shown in the example renders the column that displays text fields that allow customers to change the quantity of each book in the shopping cart. Each timeUIData
iterates through the list of books, it renders one cell in each column.The UICommand Component
The
UICommand
component performs an action when it is activated. The most common example of such a component is the button. This release supportsButton
andLink
asUICommand
component renderers.In addition to the tag attributes listed in Using the HTML Component Tags, the
commandButton
andcommandLink
tags can use these attributes:
action
, which is either a logical outcomeString
or a method-binding expression that points to a bean method that returns a logical outcomeString
. In either case, the logical outcomeString
is used by the defaultNavigationHandler
to determine what page to access when theUICommand
component is activated.actionListener
, which is a method-binding expression that points to a bean method that processes anActionEvent
fired by theUICommand
component.See Referencing a Method That Performs Navigation for more information on using the
action
attribute.See Referencing a Method That Handles an ActionEvent for details on using the
actionListener
attribute.Using the commandButton Tag
The
bookcashier.jsp
page of the Duke's Bookstore application includes acommandButton
tag. When a user clicks the button, the data from the current page is processed, and the next page is opened. Here is thecommandButton
tag frombookcashier.jsp
:Clicking the button will cause the
submit
method ofCashierBean
to be invoked because theaction
attribute references thesubmit
method of theCashierBean
backing bean. Thesubmit
method performs some processing and returns a logical outcome. This is passed to the defaultNavigationHandler
, which matches the outcome against a set of navigation rules defined in the application configuration resource file.The
value
attribute of the preceding examplecommandButton
tag references the localized message for the button's label. Thebundle
part of the expression refers to theResourceBundle
that contains a set of localized messages. TheSubmit
part of the expression is the key that corresponds to the message that is displayed on the button. For more information on referencing localized messages, see Using Localized Messages. See Referencing a Method That Performs Navigation for information on how to use theaction
attribute.Using the commandLink Tag
The
commandLink
tag represents an HTML hyperlink and is rendered as an HTML<a>
element. ThecommandLink
tag is used to submit anActionEvent
to the application. See Implementing Action Listeners for more information on action events.A
commandLink
tag must include a nestedoutputText
tag, which represents the text the user clicks to generate the event. The following tag is from thechooselocale.jsp
page from the Duke's Bookstore application.<h:commandLink id="NAmerica" action="bookstore" actionListener="#{localeBean.chooseLocaleFromLink}"> <h:outputText value="#{bundle.english}" /> </h:commandLink>
This tag will render the following HTML:
<a id="_id3:NAmerica" href="#" onclick="document.forms['_id3']['_id3:NAmerica']. value='_id3:NAmerica'; document.forms['_id3'].submit(); return false;">English</a>
Note: Notice that the
commandLink
tag will render JavaScript. If you use this tag, make sure your browser is JavaScript-enabled.
The UIData Component
The
UIData
component supports data binding to a collection of data objects. It does the work of iterating over each record in the data source. The standardTable
renderer displays the data as an HTML table. TheUIColumn
component represents a column of data within the table. Here is a portion of thedataTable
tag used by thebookshowcart.jsp
page of the Duke's Bookstore example:<h:dataTable id="books" columnClasses="list-column-center, list-column-left, list-column-right, list-column-center" footerClass="list-footer" headerClass="list-header" rowClasses="list-row-even, list-row-odd" styleClass="list-background" value="#{cart.items}" var="item"> <h:column > <f:facet name="header"> <h:outputText value="#{bundle.ItemQuantity}" /> </f:facet> <h:inputText id="quantity" size="4" value="#{item.quantity}" /> </h:column> <h:column> <f:facet name="header"> <h:outputText value="#{bundle.ItemTitle}"/> </f:facet> <h:commandLink action="#{showcart.details}"> <h:outputText value="#{item.item.title}"/> </h:commandLink> </h:column> ... <f:facet name="footer" <h:panelGroup> <h:outputText value="#{bundle.Subtotal}"/> <h:outputText value="#{cart.total}" /> <f:convertNumber type="currency" /> </h:outputText> </h:panelGroup> </f:facet> </h:dataTable>Figure 18-1 shows a data grid that this
dataTable
tag can display.
Figure 18-1 Table on
bookshowcart.jsp
PageThe example
dataTable
tag displays the books in the shopping cart as well as the number of each book in the shopping cart, the prices, and a set of buttons, which the user can click to remove books from the shopping cart.The
facet
tag inside the firstcolumn
tag renders a header for that column. The other column tags also containfacet
tags. Facets can have only one child, and so apanelGroup
tag is needed if you want to group more than one component within afacet
. Because the facet tag representing the footer includes more than one tag, thepanelGroup
is needed to group those tags.A
facet
tag is usually used to represent headers and footers. In general, a facet is used to represent a component that is independent of the parent-child relationship of the page's component tree. In the case of a data grid, header and footer data is not repeated like the other rows in the table, and therefore, the elements representing headers and footers are not updated as are the other components in the tree.This is a classic use case for a
UIData
component because the number of books might not be known to the application developer or the page author at the time the application is developed. TheUIData
component can dynamically adjust the number of rows of the table to accommodate the underlying data.The
value
attribute ofdataTable
references the data to be included in the table. This data can take the form ofAll data sources for
UIData
components have aDataModel
wrapper. Unless you explicitly construct aDataModel
, the JavaServer Faces implementation will create aDataModel
wrapper around data of any of the other acceptable types. See Writing Component Properties for more information on how to write properties for use with aUIData
component.The
var
attribute specifies a name that is used by the components within thedataTable
tag as an alias to the data referenced in thevalue
attribute ofdataTable
.In the
dataTable
tag from thebookshowcart.jsp
page, thevalue
attribute points to aList
of the books. Thevar
attribute points to a single book in that list. As theUIData
component iterates through the list, each reference toitem
points to the current book in the list.The
UIData
component also has the ability to display only a subset of the underlying data. This is not shown in the preceding example. To display a subset of the data, you use the optionalfirst
androws
attributes.The
first
attribute specifies the first row to be displayed. Therows
attribute specifies the number of rows--starting with the first row--to be displayed. By default, bothfirst
androws
are set to zero, and this causes all the rows of the underlying data to display. For example, if you wanted to display records 2 through 10 of the underlying data, you would setfirst
to 2 androws
to 9. When you display a subset of the data in your pages, you might want to consider including a link or button that causes subsequent rows to display when clicked.The
dataTable
tag also has a set of optional attributes for adding styles to the table:Each of these attributes can specify more than one style. If
columnClasses
orrowClasses
specifies more than one style, the styles are applied to the columns or rows in the order that the styles are listed in the attribute. For example, ifcolumnClasses
specifies styleslist-column-center
andlist-column-right
and if there are two columns in the table, the first column will have stylelist-column-center
, and the second column will have stylelist-column-right
.If the
style
attribute specifies more styles than there are columns or rows, the remaining styles will be assigned to columns or rows starting from the first column or row. Similarly; if thestyle
attribute specifies fewer styles than there are columns or rows, the remaining columns or rows will be assigned styles starting from the first style.The UIGraphic Component
The
UIGraphic
component displays an image. The Duke's Bookstore application uses agraphicImage
tag to display the map image on thechooselocale.jsp
page:<h:graphicImage id="mapImage" url="/template/world.jpg" alt="#{bundle.chooseLocale}" usemap="#worldMap" />The
url
attribute specifies the path to the image. It also corresponds to the local value of theUIGraphic
component so that the URL can be retrieved, possibly from a backing bean. 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
alt
attribute specifies the alternative text displayed when the user mouses over the image. In this example, thealt
attribute refers to a localized message. See Performing Localization for details on how to localize your JavaServer Faces application.The
usemap
attribute refers to the image map defined by the customMapComponent
component on the same page. See Chapter 20 for more information on the image map.The UIInput and UIOutput Components
The
UIInput
component displays a value to the user and allows the user to modify this data. The most common example is a text field. TheUIOutput
component displays data that cannot be modified. The most common example is a label.The
UIInput
andUIOutput
components can each be rendered in four ways. Table 18-3 lists the renderers ofUIInput
andUIOutput
. Recall from The Component Rendering Model that the tags are composed of the component and the renderer. For example, theinputText
tag refers to aUIInput
component that is rendered with theText
renderer
The
UIInput
component supports the following tag attributes in addition to the tag attributes described at the beginning of Using the HTML Component Tags. TheUIOutput
component supports the first of the following tag attributes in addition to those listed in Using the HTML Component Tags.
converter
: Identifies a converter that will be used to convert the component's local data. See Using the Standard Converters for more information on how to use this attribute.validator
: A method-binding expression pointing to a backing bean method that performs validation on the component's data. See Referencing a Method That Performs Validation for an example of usingvalidator
.valueChangeListener
: A method-binding expression that points to a backing bean method that handles the event of entering a value in this component. See Referencing a Method That Handles a ValueChangeEvent for an example of usingvalueChangeListener
.The rest of this section explains how to use selected tags listed in the Table 18-3. The other tags are written in a similar way.
Using the outputText and inputText Tags
The
Text
renderer can render bothUIInput
andUIOutput
components. TheinputText
tag displays and accepts a single-line string. TheoutputText
tag displays a single-line string. This section shows you how to use theinputText
tag. TheoutputText
tag is written in a similar way.Here is an example of an
inputText
tag from thebookcashier.jsp
page:<h:inputText id="name" size="50" value="#{cashier.name}" required="true"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText>The
value
attribute refers to thename
property of theCashierBean
. This property holds the data for thename
component. After the user submits the form, the value of thename
property inCashierBean
will be set to the text entered in the field corresponding to this tag.The
required
attribute causes the page to reload, with errors displayed if the user does not enter a value in thename
text field. See Requiring a Value for more information on requiring input for a component.Using the outputLabel Tag
The
outputLabel
tag is used to attach a label to a specified input field for accessibility purposes. Thebookcashier.jsp
page uses anoutputLabel
tag to render the label of a checkbox:<h:selectBooleanCheckbox id="fanClub" rendered="false" binding="#{cashier.specialOffer}" /> <h:outputLabel for="fanClubLabel" rendered="false" binding="#{cashier.specialOfferText}" > <h:outputText id="fanClubLabel" value="#{bundle.DukeFanClub}" /> </h:outputLabel>The
for
attribute of theoutputLabel
tag maps to theid
of the input field to which the label is attached. TheoutputText
tag nested inside theoutputLabel
tag represents the actual label component. Thevalue
attribute on theoutputText
tag indicates the label that is displayed next to the input field.Using the outputLink Tag
The
outputLink
tag is used to render a hyperlink that, when clicked, loads another page but does not generate anActionEvent
. You should use this tag instead of thecommandLink
tag if you always want the URL--specified by theoutputLink
tag'svalue
attribute--to open and do not have to perform any processing when the user clicks on the link. The Duke's Bookstore application does not utilize this tag, but here is an example of it:<h:outputLink value="javadocs"> <f:verbatim>Documentation for this demo</f:verbatim> </h:outputLink>As shown in this example, the
outputLink
tag requires a nestedverbatim
tag, which identifies the text the user clicks to get to the next page.You can use the
verbatim
tag on its own when you want to simply output some text on the page.Using the outputFormat Tag
The
outputFormat
tag allows a page author to display concatenated messages as aMessageFormat
pattern, as described in the API documentation forjava.text.MessageFormat
(seehttp://java.sun.com/j2se/1.4.2/docs/api/java/text/MessageFormat.html
). Here is an example of anoutputFormat
tag from thebookshowcart.jsp
page of the Duke's Bookstore application:<h:outputFormat value="#{bundle.CartItemCount}"> <f:param value="#{cart.numberOfItems}"/> </h:outputFormat>The
value
attribute specifies theMessageFormat
pattern. Theparam
tag specifies the substitution parameters for the message.In the example
outputFormat
tag, thevalue
for the parameter maps to the number of items in the shopping cart. When the message is displayed in the page, the number of items in the cart replaces the{0}
in the message corresponding to theCartItemCount
key in thebundle
resource bundle:This message can be broken down into three possibilities:
The value of the parameter replaces the
{0}
from the message in the sentence in the third bullet. This is an example of a value-binding-enabled tag attribute accepting a complex JSP 2.0 EL expression.An
outputFormat
tag can include more than oneparam
tag for those messages that have more than one parameter that must be concatenated into the message. If you have more than one parameter for one message, make sure that you put theparam
tags in the proper order so that the data is inserted in the correct place in the message.A page author can also hardcode the data to be substituted in the message by using the
value
attribute on theparam
tag.Using the inputSecret Tag
The
inputSecret
tag renders an<input type="password">
HTML tag. When the user types a string into this field, a row of asterisks is displayed instead of the text the user types. The Duke's Bookstore application does not include this tag, but here is an example of one:In this example, the
redisplay
attribute is set tofalse
. 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
UIPanel
component is used as a layout container for its children. When you use the renderers from the HTML render kit, aUIPanel
is rendered as an HTML table. This component differs fromUIData
in thatUIData
can dynamically add or delete rows to accommodate the underlying data source, whereas aUIPanel
must have the number of rows predetermined. Table 18-4 lists all the renderers and tags corresponding to theUIPanel
component.
The
panelGrid
tag is used to represent an entire table. ThepanelGroup
tag is used to represent rows in a table. Other UI component tags are used to represent individual cells in the rows.The
panelGrid
tag has a set of attributes that specify CSS stylesheet classes:columnClasses
,footerClass
,headerClass
,panelClass
, androwClasses
. These stylesheet attributes are not required. It also has acolumns
attribute. Thecolumns
attribute is required if you want your table to have more than one column because thecolumns
attribute tells the renderer how to group the data in the table.If a
headerClass
is specified, thepanelGrid
must have a header as its first child. Similarly, if afooterClass
is specified, thepanelGrid
must have a footer as its last child.The Duke's Bookstore application includes three
panelGrid
tags on thebookcashier.jsp
page. Here is a portion of one of them:<h:panelGrid columns="3" headerClass="list-header" rowClasses="list-row-even, list-row-odd" styleClass="list-background" title="#{bundle.Checkout}"> <f:facet name="header"> <h:outputText value="#{bundle.Checkout}"/> </f:facet> <h:outputText value="#{bundle.Name}" /> <h:inputText id="name" size="50" value="#{cashier.name}" required="true"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText> <h:message styleClass="validationMessage" for="name"/> <h:outputText value="#{bundle.CCNumber}"/> <h:inputText id="ccno" size="19" converter="creditCardConverter" required="true"> <bookstore:formatValidator formatPatterns="9999999999999999| 9999 9999 9999 9999|9999-9999-9999-9999"/> </h:inputText> <h:message styleClass="validationMessage" for="ccno"/> ... </h:panelGrid>This
panelGrid
is rendered to a table that contains controls for the customer of the bookstore to input personal information. ThispanelGrid
uses stylesheet classes to format the table. The CSS classes are defined in thestylesheet.css
file in the <INSTALL
>/j2eetutorial14/examples/web/bookstore6/web/
directory. Thelist-header
definition isBecause the
panelGrid
tag specifies aheaderClass
, thepanelGrid
must contain a header. The examplepanelGrid
uses afacet
tag for the header. Facets can have only one child, and so apanelGroup
tag is needed if you want to group more than one component within afacet
. Because the examplepanelGrid
tag has only one cell of data, apanelGroup
tag is not needed.A
panelGroup
tag can also be used to encapsulate a nested tree of components so that the tree of components appears as a single component to the parent component.The data represented by the nested component tags is grouped into rows according to the value of the columns attribute of the
panelGrid
tag. Thecolumns
attribute in the example is set to"3"
, and therefore the table will have three columns. In which column each component is displayed is determined by the order that the component is listed on the page modulo 3. So if a component is the fifth one in the list of components, that component will be in the 5 modulo 3 column, or column 2.The UISelectBoolean Component
The
UISelectBoolean
class defines components that have aboolean
value. TheselectBooleanCheckbox
tag is the only tag that JavaServer Faces technology provides for representingboolean
state. The Duke's Bookstore application includes aselectBooleanCheckbox
tag on thebookcashier.jsp
page:<h:selectBooleanCheckbox id="fanClub" rendered="false" binding="#{cashier.specialOffer}" /> <h:outputLabel for="fanClubLabel" rendered="false" binding="#{cashier.specialOfferText}"> <h:outputText id="fanClubLabel" value="#{bundle.DukeFanClub}" /> </h:outputLabel>This example tag displays a checkbox to allow users to indicate whether they want to join the Duke Fan Club. The label for the checkbox is rendered by the
outputLabel
tag. The actual text is represented by the nestedoutputText
tag. Binding a Component Instance to a Bean Property discusses this example in more detail.The UISelectMany Component
The
UISelectMany
class defines components that allow the user to select zero or more values from a set of values. This component can be rendered as a set of checkboxes, a list box, or a menu. This section explains theselectManyCheckbox
tag. TheselectManyListbox
tag andselectManyMenu
tag are written in a similar way.A list box differs from a menu in that it displays a subset of items in a box, whereas a menu displays only one item at a time until you select the menu. The
size
attribute of theselectManyListbox
tag determines the number of items displayed at one time. The list box includes a scrollbar for scrolling through any remaining items in the list.Using the selectManyCheckbox Tag
The
selectManyCheckbox
tag renders a set of checkboxes, with each checkbox representing one value that can be selected. Duke's Bookstore uses aselectManyCheckbox
tag on thebookcashier.jsp
page to allow the user to subscribe to one or more newsletters:<h:selectManyCheckbox id="newsletters" layout="pageDirection" value="#{cashier.newsletters}"> <f:selectItems value="#{newsletters}"/> </h:selectManyCheckbox>The
value
attribute of theselectManyCheckbox
tag identifies theCashierBean
backing bean property,newsletters
, for the current set of newsletters. This property holds the values of the currently selected items from the set of checkboxes.The
layout
attribute indicates how the set of checkboxes are arranged on the page. Because layout is set topageDirection
, the checkboxes are arranged vertically. The default islineDirection
, which aligns the checkboxes horizontally.The
selectManyCheckbox
tag must also contain a tag or set of tags representing the set of checkboxes. To represent a set of items, you use theselectItems
tag. To represent each item individually, you use aselectItem
tag for each item. UISelectItem, UISelectItems, and UISelectItemGroup explains these two tags in more detail.The UIMessage and UIMessages Components
The
UIMessage
andUIMessages
components are used to display error messages. Here is an examplemessage
tag from theguessNumber
application, discussed in Steps in the Development Process:<h:inputText id="userNo" value="#{UserNumberBean.userNumber}" validator="#{UserNumberBean.validate}"/> ... <h:message style="color: red; font-family: 'New Century Schoolbook', serif; font-style: oblique; text-decoration: overline" id="errors1" for="userNo"/>The
for
attribute refers to the ID of the component that generated the error message. Themessage
tag will display the error message wherever it appears on the page.The
style
attribute allows you to specify the style of the text of the message. In the example in this section, the text will be red, New Century Schoolbook, serif font family, and oblique style, and a line will appear over the text.If you use the
messages
tag instead of themessages
tag, all error messages will display.The UISelectOne Component
A
UISelectOne
component allows the user to select one value from a set of values. This component can be rendered as a list box, a radio button, or a menu. This section explains theselectOneMenu
tag. TheselectOneRadio
andselectOneMenu
tags are written in a similar way. TheselectOneListbox
tag is similar to theselectOneMenu
tag except thatselectOneListbox
defines asize
attribute that determines how many of the items are displayed at once.Using the selectOneMenu Tag
The
selectOneMenu
tag 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 theselectOneMenu
tag. Here is an exampleselectOneMenu
tag from thebookcashier.jsp
page of the Duke's Bookstore application:<h:selectOneMenu id="shippingOption" required="true" value="#{cashier.shippingOption}"> <f:selectItem itemValue="2" itemLabel="#{bundle.QuickShip}"/> <f:selectItem itemValue="5" itemLabel="#{bundle.NormalShip}"/> <f:selectItem itemValue="7" itemLabel="#{bundle.SaverShip}"/> </h:selectOneMenu>The
value
attribute of theselectOneMenu
tag maps to the property that holds the currently selected item's value.Like the
selectOneRadio
tag, theselectOneMenu
tag must contain either aselectItems
tag or a set ofselectItem
tags for representing the items in the list. The next section explains these two tags.UISelectItem, UISelectItems, and UISelectItemGroup
UISelectItem
andUISelectItems
represent components that can be nested inside aUISelectOne
or aUISelectMany
component. TheUISelectItem
is associated with aSelectItem
instance, which contains the value, label, and description of a single item in theUISelectOne
orUISelectMany
component.The
UISelectItems
instance represents either of the following:Figure 18-2 shows an example of a list box constructed with a
SelectItems
component representing twoSelectItemGroup
instances, each of which represents two categories of beans. Each category is an array ofSelectItem
instances.
Figure 18-2 An Example List Box Created Using
SelectItemGroup
InstancesThe
selectItem
tag represents aUISelectItem
component. TheselectItems
tag represents aUISelectItems
component. You can use either a set ofselectItem
tags or a singleselectItems
tag within yourselectOne
orselectMany
tag.The advantages of using the
selectItems
tag are as follows:
- You can represent the items using different data structures, including
Array
,Map
,List
, andCollection
. The data structure is composed ofSelectItem
instances orSelectItemGroup
instances.- You can concatenate different lists together into a single
UISelectMany
orUISelectOne
component and group the lists within the component, as shown in Figure 18-2.- You can dynamically generate values at runtime.
The advantages of using
selectItem
are as follows:For more information on writing component properties for the
UISelectItems
components, see Writing Component Properties. The rest of this section shows you how to use theselectItems
andselectItem
tags.Using the selectItems Tag
Here is the
selectManyCheckbox
tag from the section The UISelectMany Component:<h:selectManyCheckbox id="newsletters" layout="pageDirection" value="#{cashier.newsletters}"> <f:selectItems value="#{newsletters}"/> </h:selectManyCheckbox>The
value
attribute of theselectItems
tag is bound to thenewsletters
managed bean, which is configured in the application configuration resource file. Thenewsletters
managed bean is configured as list:<managed-bean> <managed-bean-name>newsletters</managed-bean-name> <managed-bean-class> java.util.ArrayList</managed-bean-class> <managed-bean-scope>application</managed-bean-scope> <list-entries> <value-class>javax.faces.model.SelectItem</value-class> <value>#{newsletter0}</value> <value>#{newsletter1}</value> <value>#{newsletter2}</value> <value>#{newsletter3}</value> </list-entries> </managed-bean> <managed-bean> <managed-bean-name>newsletter0</managed-bean-name> <managed-bean-class> javax.faces.model.SelectItem</managed-bean-class> <managed-bean-scope>none</managed-bean-scope> <managed-property> <property-name>label</property-name> <value>Duke's Quarterly</value> </managed-property> <managed-property> <property-name>value</property-name> <value>200</value> </managed-property> </managed-bean> ...As shown in the
managed-bean
element, theUISelectItems
is a collection ofSelectItem
instances. See Initializing Array and List Properties for more information on configuring collections as beans.You can also create the list corresponding to a
UISelectMany
orUISelectOne
component programmatically in the backing bean. See Writing Component Properties for information on how to write a backing bean property corresponding to aUISelectMany
orUISelectOne
component.The arguments to the
SelectItem
constructor are:UISelectItems Properties describes in more detail how to write a backing bean property for a
UISelectItems
component.Using the selectItem Tag
The
selectItem
tag represents a single item in a list of items. Here is the example from Using the selectOneMenu Tag:<h:selectOneMenu id="shippingOption" required="true" value="#{cashier.shippingOption"> <f:selectItem itemValue="2" itemLabel="#{bundle.QuickShip}"/> <f:selectItem itemValue="5" itemLabel="#{bundle.NormalShip}"/> <f:selectItem itemValue="7" itemLabel="#{bundle.SaverShip}"/> </h:selectOneMenu>The
itemValue
attribute represents the default value of theSelectItem
instance. TheitemLabel
attribute represents theString
that appears in the drop-down menu component on the page.The
itemValue
anditemLabel
attributes are value-binding-enabled, meaning that they can use value-binding expressions to refer to values in external objects. They can also define literal values, as shown in the exampleselectOneMenu
tag.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.