?? combolistboxbinding.jsp
字號:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- tpl:insert page="/template/jsfTemplate.jtpl" --%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="../theme/Master.css" rel="stylesheet" type="text/css">
<LINK href="../theme/new.css" rel="stylesheet">
<LINK rel="stylesheet" type="text/css" href="../theme/stylesheet.css" title="Style">
<%-- tpl:put name="headarea" --%>
<title>comboListBoxBinding.jsp</title>
<%-- /tpl:put --%>
<LINK rel="stylesheet" type="text/css" href="/JSFandSDO/theme/stylesheet.css" title="Style">
</HEAD>
<f:view>
<BODY>
<TABLE class="title" cellpadding="0">
<TBODY>
<TR>
<TD class="noBorder" background="../images/gradient.jpg"
align="CENTER"><IMG border="0" src="../images/title.jpg"></TD>
</TR>
<TR>
<TD class="noBorder" align="RIGHT"><A href="/JSFandSDO">Return to Main Menu</A></TD>
</TR>
</TBODY>
</TABLE>
<%-- tpl:put name="bodyarea" --%>
<hx:scriptCollector id="scriptCollector1">
<h:form styleClass="form" id="form1">
<H2>Adding Choices to selection lists - combo and List Boxes</H2>
This sample consists of a single JSF page containing a combo box, a multiple-select list
box, and a single-select list box. Each selection box has its Value attribute bound to a backing JavaBean which stores the user selected values. The choices in each selection box can be supplied in a variety of ways and are demonstrated below. When you submit the page the values you select are stored into a backing JavaBean and displayed on the page.<BR>
<BR>
<TABLE cellpadding="3">
<TBODY>
<TR>
<TH class="thColor25">Illustration</TH>
<TH class="th75">Description</TH>
</TR>
<TR>
<TD class="tdColor25"><B>Combo Box with Static Choices<BR>
<BR>
Select a Color: </B><BR>
<%-- The value attribute of the h:selectOneMenu tells where the selected value will be stored.
In this case the "color" property of ComboListValuesBean.java --%>
<h:selectOneMenu styleClass="selectOneMenu" id="colorMenu" value="#{pc_ComboListBoxBinding.comboListValues.color}">
<%-- The choices in the combo box can be created directly in the JSP code as seen here. --%>
<f:selectItem itemValue="red" itemLabel="Red" />
<f:selectItem itemValue="green" itemLabel="Green" />
<f:selectItem itemValue="blue" itemLabel="Blue" />
<f:selectItem itemValue="orange" itemLabel="Orange" />
<f:selectItem itemValue="yellow" itemLabel="Yellow" />
</h:selectOneMenu><BR>
<BR>
<hx:commandExButton type="submit" value="Submit Page" styleClass="commandExButton2" id="button2"></hx:commandExButton>
<BR>
<BR>
<B>Stored Value: </B>
<c:choose>
<c:when test="${pc_ComboListBoxBinding.comboListValues.color == null}">
<c:out value="<no selection>" />
</c:when>
</c:choose>
<h:outputText styleClass="outputText" id="colorOutput" value="#{pc_ComboListBoxBinding.comboListValues.color}"></h:outputText></TD>
<TD class="td75">The combo box choices are supplied
directly in the JSP page using multiple <SPAN class="tag">selectItem</SPAN>
tags. You can add these tags to your page by going to the Properties view for the combo box and selecting "Add Choice". Then you can supply a label and value for the new choice. The Value attribute of the combo box is bound to the "color" attribute of the <I>ComboListValues </I>JavaBean.<BLOCKQUOTE>
<P class="code">Example:<BR>
<h:selectOneMenu styleClass="selectOneMenu" id="colorMenu" value="#{pc_ComboListBoxBinding.comboListValues.color}"><BR>
<SPAN class="indent"><f:selectItem
itemValue="red" itemLabel="Red" /></SPAN><BR>
<SPAN class="indent"><f:selectItem itemValue="green" itemLabel="Green" /></SPAN><BR>
<SPAN class="indent"><f:selectItem itemValue="blue" itemLabel="Blue" /></SPAN><BR>
<SPAN class="indent"><f:selectItem itemValue="orange" itemLabel="Orange" /></SPAN><BR>
<SPAN class="indent"><f:selectItem itemValue="yellow" itemLabel="Yellow" /></SPAN><BR>
</h:selectOneMenu></P></BLOCKQUOTE></TD>
</TR>
<TR>
<TD class="tdColor25"><B>Multiple Select List Box with Data-Bound Choices (array of Strings)<BR>
<BR>
Select one or more Shapes: </B><BR>
<%-- The value attribute of the h:selectManyListbox tells where the selected values will be stored.
In this case the "shape" property of ComboListValuesBean.java --%>
<h:selectManyListbox styleClass="selectManyListbox" id="shapeMenu" value="#{pc_ComboListBoxBinding.comboListValues.shape}">
<%-- Use the selectItems tag to add choices to the List Box with an Array of Strings from the Bean.
In this case we use the "selectitems" keyword and the "toArray" keyword to bind to the String array.
Those keywords are always needed unless binding directly to SelectItem objects from the Bean. --%>
<f:selectItems value="#{selectitems.pc_ComboListBoxBinding.comboList.shapes.shapes.toArray}" />
</h:selectManyListbox><BR>
<BR>
<hx:commandExButton type="submit" value="Submit Page" styleClass="commandExButton2" id="button3"></hx:commandExButton>
<BR>
<BR>
<B>Stored Value(s): </B>
<c:choose>
<c:when test="${pc_ComboListBoxBinding.comboListValues.selected == null}">
<c:out value="<no selection>" />
</c:when>
</c:choose><h:outputText styleClass="outputText" id="shapeOutput" value="#{pc_ComboListBoxBinding.comboListValues.selected}"></h:outputText>
</TD>
<TD class="td75">The multiple-select list box choices are supplied by
binding a single <SPAN class="tag">selectItems</SPAN> tag to an array of
Strings stored in a JavaBean. You can add a
<SPAN class="tag">selectItems</SPAN> tag to your page by selecting "Add Set of
Choices" in the Properties view of the list box. Then by clicking the small icon that appears you can browse for a data object to bind your tag to. The Value attribute of the list box is bound to the "shape" array of the <I>ComboListValues </I>JavaBean.<BLOCKQUOTE>
<P class="code">Example:<BR>
<h:selectManyListbox styleClass="selectManyListbox" id="shapeMenu" value="#{pc_ComboListBoxBinding.comboListValues.shape}"><BR>
<SPAN class="indent"><f:selectItems value="#{selectitems.pc_ComboListBoxBinding.comboList.shapes.shapes.toArray}" /></SPAN><BR>
</h:selectManyListbox></P>
</BLOCKQUOTE>
An array of Strings is not an expected value for the
<SPAN class="tag">selectItems</SPAN> tag. To make the choices display properly
the<B> selectitems</B> and <B>toArray </B>keywords are added to the value binding statement. This is done automatically for you when you bind to an array of Strings using the JSF tools.</TD>
</TR>
<TR>
<TD class="tdColor25"><B>Single Select List Box with Data-Bound Choices (Collection)<BR>
<BR>
Select a Month: </B><BR>
<h:selectOneListbox styleClass="selectOneListbox" id="monthMenu" value="#{pc_ComboListBoxBinding.comboListValues.month}" size="4">
<%-- Use the selectItems tag to populate listbox with Collection of SelectItem objects. In this
case we can bind directly to the bean because we are using SelectItem objects --%>
<f:selectItems value="#{pc_ComboListBoxBinding.comboList.months}" />
</h:selectOneListbox><BR>
<BR>
<hx:commandExButton type="submit" value="Submit Page" styleClass="commandExButton2" id="button1"></hx:commandExButton>
<BR>
<BR>
<B>Stored Value</B>:
<c:choose>
<c:when test="${pc_ComboListBoxBinding.comboListValues.month == null}">
<c:out value="<no selection>" />
</c:when>
</c:choose><h:outputText styleClass="outputText" id="monthOutput" value="#{pc_ComboListBoxBinding.comboListValues.month}"></h:outputText></TD>
<TD class="td75">The single-select list box choices are supplied by
binding a single <SPAN class="tag">selectItems</SPAN> tag to a Collection of
<I>SelectItem</I> objects stored in a JavaBean. You can add a
<SPAN class="tag">selectItems</SPAN> tag to your page by selecting "Add Set of
Choices" in the Properties view of the list box. Then by clicking the small icon that appears you can browse for a data object to bind your tag to. The Value attribute of the list box is bound to the "month" attribute of the <I>ComboListValues </I>JavaBean. In this example our "month" attribute has a pre-set value which is why January is already selected when this page first loads.<BLOCKQUOTE><P class="code">Example:<BR>
<h:selectOneListbox styleClass="selectOneListbox" id="monthMenu" value="#{pc_ComboListBoxBinding.comboListValues.month}" size="4"><BR>
<SPAN class="indent"><f:selectItems value="#{pc_ComboListBoxBinding.comboList.months}" /></SPAN><BR>
</h:selectOneListbox></P></BLOCKQUOTE></TD>
</TR>
</TBODY>
</TABLE>
<H4>How It works:</H4>The name-value pairs that make up the choices in each selection box can be
created in the JSP file using multiple <SPAN class="tag">selectItem</SPAN> tags or they can be supplied by a data source, such as a JavaBean, using a single <SPAN class="tag">selectItems</SPAN> tag.<BR>
<BR>
The <SPAN class="tag">selectItems</SPAN> tag expects to be bound to certain types of data. These include a single <I>SelectItem</I> instance, a Collection of <I>SelectItem</I> instances, an array of <I>SelectItem</I> Instances, or a Map with its entries representing <I>SelectItem</I> labels and values.
To bind to other types of data, such as an array of Strings, the <B>selectitems</B> and <B>toArray</B> keywords are added to your value binding statement as seen in the "Shape" multiple-select list box. The JSF tools will automatically create the proper type of value binding statement when you use them to create your choices. Choices can be added from the Properties view of each component.<BR>
<BR>
Each selection box in this example is bound to a backing JavaBean which stores the user selected values, by settings its Value attribute. The "Value" refers to a property in the JavaBean. To set the value of the Selection Box you can drag a data object from the Page Data View onto your component or enter the value binding in the Value field of its Properties View. To bind a component to a backing JavaBean, its object type (int, String, boolean, etc) must match the object type of the bean property you are binding to. Value binding statements can also be used to set the initial selection of a selection box when the page is first loaded.<BR>
<BR>
Multiple-select list boxes should be bound to an array or List of the data type you want to store their values as. In this example the "Shape" list box is bound to an array of Strings with its size equal to the number of items selected.<H4>Files of Interest:</H4>
<UL>
<LI><B>WebContent/comboListBoxBinding/comboListBoxBinding.jsp</B> - (This Page) Here you will find the JSF tags to place the three boxes on the JSP page and the value bindings which are used to add the choices and store the user selected values.</LI>
<LI><B>src/com.ibm.samples.comboListBox.ComboListBoxBean.java</B> - The
JavaBean where the array of Strings and Collection of <I>SelectItem</I>
objects are created.</LI>
<LI><B>src/com.ibm.samples.comboListBox.ComboListValuesBean.java</B> - The
JavaBean where the current values of the form are stored and
retrieved from.</LI>
<LI><B>src/pagecode.comboListBoxBinding.java</B> - The Page Code file.</LI>
</UL>
</h:form>
</hx:scriptCollector>
<%-- /tpl:put --%>
<TABLE class="title" cellpadding="0">
<TBODY>
<TR>
<TD class="noBorder" align="RIGHT"><A href="/JSFandSDO">Return to Main Menu</A></TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</f:view>
</HTML><%-- /tpl:insert --%>
<%-- jsf:pagecode language="java" location="/src/pagecode/comboListBoxBinding/ComboListBoxBinding.java" --%><%-- /jsf:pagecode --%>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -