选择类的标签可略分为单选卷标与多选卷标,依外型的不同可以分为单选钮(Radio)、复选框(CheckBox)、列示方块(ListBox)与选单(Menu),以下分别先作简单的说明。 <h:selectBooleanCheckbox> 在视图上呈现一个复选框,例如: 我同意 <h:selectBooleanCheckbox value="#/{user.aggree/}"/> value所绑定的属性必须接受与传回boolean型态。这个组件在网页上呈现的外观如下:
<h:selectOneRadio>、<h:selectOneListbox>、<h: selectOneMenu> 这三个标签的作用,是让使用者从其所提供的选项中选择一个项目,所不同的就是其外观上的差别,例如: <h:selectOneRadio value="#{user.education}"> <f:selectItem itemLabel="高中" itemValue="高中"/> <f:selectItem itemLabel="大学" itemValue="大学"/> <f:selectItem itemLabel="研究所以上" itemValue="研究所以上"/> </h:selectOneRadio><p> value所绑定的属性可以接受字符串以外的型态或是自订型态,但记得如果是必须转换的型态或自订型态,必须搭配 标准转换器 或 自订转换器 来转换为对象,<h:selectOneRadio>的外观如下:
您也可以设定layout属性,可设定的属性是lineDirection、pageDirection,预设是lineDirection,也就是由左到右来排列选项,如果设定为pageDirection,则是由上至下排列选项,例如设定为: <h:selectOneRadio layout="pageDirection" value="#{user.education}"> <f:selectItem itemLabel="高中" itemValue="高中"/> <f:selectItem itemLabel="大学" itemValue="大学"/> <f:selectItem itemLabel="研究所以上" itemValue="研究所以上"/> </h:selectOneRadio><p> 则外观如下:
<h:selectOneListbox>、<h:selectOneMenu>的设定方法类似于<h: selectOneRadio>,以下分别列出<h:selectOneListbox>、<h: selectOneMenu>的外观:
<h:selectManyCheckbox>、<h:selectManyListbox>、<h: selectManyMenu> 这三个卷标提供使用者复选项目的功能,一个<h:selectManyCheckbox>例子如下: <h:selectManyCheckbox layout="pageDirection" value="#{user.preferColors}"> <f:selectItem itemLabel="红" itemValue="false"/> <f:selectItem itemLabel="黄" itemValue="false"/> <f:selectItem itemLabel="蓝" itemValue="false"/> </h:selectManyCheckbox><p> value所绑定的属性必须是数组或集合(Collection)对象,在这个例子中所使用的是boolean数组,例如: UserBean.java package onlyfun.caterpillar;
public class UserBean { private boolean[] preferColors;
public boolean[] getPreferColors() { return preferColors; }
public void setPreferColors(boolean[] preferColors) { this.preferColors = preferColors; }
...... } 如果是其它型态的对象,必要时必须搭配转换器(Converter)进行字符串与对象之间的转换。 下图是<h:selectManyCheckbox>的外观,这是将layout设定为pageDirection的外观:
<h:selectManyListbox>的设定方法类似,其外观如下:
<h:selectManyMenu>在不同的浏览器中会有不同的外观,在Mozilla Firefox中是这样的:
在Internet Explorer则是这样的:
|