In JSF, <h:selectManyListbox /> tag is used to render a multiple select listbox – HTML select element with “multiple” and “size” attribute.

//JSF...
<h:selectManyListbox value="#{user.favFood1}">
<f:selectItem itemValue="Fry Checken" itemLabel="Food1 - Fry Checken" />
<f:selectItem itemValue="Tomyam Soup" itemLabel="Food1 - Tomyam Soup" />
<f:selectItem itemValue="Mixed Rice" itemLabel="Food1 - Mixed Rice" />
</h:selectManyListbox>
//HTML output...
<select name="j_idt6:j_idt8" multiple="multiple" size="3">
<option value="Fry Checken">Food1 - Fry Checken</option>
<option value="Tomyam Soup">Food1 - Tomyam Soup</option>
<option value="Mixed Rice">Food1 - Mixed Rice</option>
</select>

h:selectManyListbox example

A JSF 2.0 example to show the use of “h:selectManyListbox” tag to render multiple select listbox, and populate the data in 3 different ways :

  • Hardcoded value in “f:selectItem” tag.
  • Generate values with a Map and put it into “f:selectItems” tag.
  • Generate values with an Object array and put it into “f:selectItems” tag, then represent the value with “var” attribute.

1. Backing Bean

A backing bean to hold and generate data for the multiple select listbox values. The property to hold the multi-selected listbox value, must be a type of Collection or Array; Otherwise it will hits the following error message

WARNING: Target model Type is no a Collection or Array
javax.faces.FacesException: Target model Type is no a Collection or Array
package com.mkyong;

import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped; @ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{ public String[] favFood1;
public String[] favFood2;
public String[] favFood3; //getter and setter methods... public String getFavFood1InString() {
return Arrays.toString(favFood1);
} public String getFavFood2InString() {
return Arrays.toString(favFood2);
} public String getFavFood3InString() {
return Arrays.toString(favFood3);
} //Generated by Map
private static Map<String,Object> food2Value;
static{
food2Value = new LinkedHashMap<String,Object>();
food2Value.put("Food2 - Fry Checken", "Fry Checken"); //label, value
food2Value.put("Food2 - Tomyam Soup", "Tomyam Soup");
food2Value.put("Food2 - Mixed Rice", "Mixed Rice");
} public Map<String,Object> getFavFood2Value() {
return food2Value;
} //Generated by Object array
public static class Food{
public String foodLabel;
public String foodValue; public Food(String foodLabel, String foodValue){
this.foodLabel = foodLabel;
this.foodValue = foodValue;
} public String getFoodLabel(){
return foodLabel;
} public String getFoodValue(){
return foodValue;
} } public Food[] food3List; public Food[] getFavFood3Value() { food3List = new Food[3];
food3List[0] = new Food("Food3 - Fry Checken", "Fry Checken");
food3List[1] = new Food("Food3 - Tomyam Soup", "Tomyam Soup");
food3List[2] = new Food("Food3 - Mixed Rice", "Mixed Rice"); return food3List;
} }

2. JSF Page

A JSF page to demonstrate the use “h:selectManyListbox” tag.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:body> <h1>JSF 2 multi-select listbox example</h1>
<h:form> 1. Hard-coded with "f:selectItem" :
<h:selectManyListbox value="#{user.favFood1}">
<f:selectItem itemValue="Fry Checken" itemLabel="Food1 - Fry Checken" />
<f:selectItem itemValue="Tomyam Soup" itemLabel="Food1 - Tomyam Soup" />
<f:selectItem itemValue="Mixed Rice" itemLabel="Food1 - Mixed Rice" />
</h:selectManyListbox> <br /><br /> 2. Generated by Map :
<h:selectManyListbox value="#{user.favFood2}">
<f:selectItems value="#{user.favFood2Value}" />
</h:selectManyListbox> <br /><br /> 3. Generated by Object array and iterate with var :
<h:selectManyListbox value="#{user.favFood3}">
<f:selectItems value="#{user.favFood3Value}" var="f"
itemLabel="#{f.foodLabel}" itemValue="#{f.foodValue}" />
</h:selectManyListbox> <br /><br /> <h:commandButton value="Submit" action="result" />
<h:commandButton value="Reset" type="reset" /> </h:form> </h:body> </html>

result.xhtml…

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
> <h:body> <h1>JSF 2 multi-select listbox example</h1> <h2>result.xhtml</h2> <ol>
<li>user.favFood1 : #{user.favFood1InString}</li>
<li>user.favFood2 : #{user.favFood2InString}</li>
<li>user.favFood3 : #{user.favFood3InString}</li>
</ol>
</h:body> </html>

3. Demo

When “submit” button is clicked, link to “result.xhtml” page and display the submitted multi-selected listbox values.

How to pre-select multiple listbox values ?

The value of “f:selectItems” tag is selected if it matched to the “value” of “h:selectManyListbox” tag. In above example, if you set favFood1 property to “Fry Checken” and “Tomyam Soup” :

@ManagedBean(name="user")
@SessionScoped
public class UserBean{ public String[] favFood1 = {"Fry Checken", "Tomyam Soup"}; //...

The “favFood1″ listbox values, “Fry Checken” and “Tomyam Soup” are selected by default.

JSF 2 multiple select listbox example的更多相关文章

  1. JSF 2 multiple select dropdown box example

    In JSF, <h:selectManyMenu /> tag is used to render a multiple select dropdown box – HTML selec ...

  2. 目录窗口多选Multiple Select in Catalog Window or arccatalog

    目录窗口多选Multiple Select in Catalog Window or arccatalog 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#q ...

  3. html multiple select option 分组

    普通html方式展示<select name="viewType" style="width: 100%;height: 300px;" multiple ...

  4. JSF 2 listbox example

    In JSF, <h:selectOneListbox /> tag is used to render a single select listbox – HTML select ele ...

  5. ListBox控件例子

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.c ...

  6. Web页面中两个listbox的option的转移

    Html: <div><span>所选时间:</span><select id="xuanyongTimelb" style=" ...

  7. 前端插件之Bootstrap Dual Listbox使用

    工欲善其事,必先利其器 对于很多非专业前端开发来说写页面是非常痛苦的,借助框架或插件往往能够达到事半功倍的效果,本系列文章会介绍我在运维系统开发过程中用到的那些顺手的前端插件,如果你是想写XX管理系统 ...

  8. select框宽度与高度设置(实用版)

    在IE中只能使用 font-size: 限制 select 的高度.   同时使用 width:200px 限制宽度   size="20" 表示最多显示20个选项,超过20的需要 ...

  9. js select级联,上面分类,下面是内容

    js select级联,上面分类,下面是内容. js级联效果如下: 分类: 请选择 水果 蔬菜 其他 内容: // html和js代码如下:     <html>      <hea ...

随机推荐

  1. 方法Equals和操作符==的区别

    http://www.codeproject.com/Articles/584128/What-is-the-difference-between-equalsequals-and-Eq When w ...

  2. eclipse运行emulator时,PANIC:Could not open emulator的解决办法

    使用eclipse启动emulator的时候,出现PANIC:Could not open emulator,模拟器无法正常的运行. 经过搜索得知,因为我的SDK的环境变量出问题,需要重新配置下环境变 ...

  3. DrawDib函数组的使用

    Microsoft的针对与设备无关位图(DIB位图),在其WIN32 SDK的Multimedia中提供了一组绘制DIB位图的高性能函数组──DrawDib函数组.DrawDib函数组是一组不依赖于图 ...

  4. cocoapods 终极方案

    最近各种错误, 全部刷新 再说 sudo gem install -n /usr/local/bin cocoapods $ sudo gem update --system // 先更新gem $ ...

  5. shell脚本实例

    备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...

  6. 【转】移动web资源整理

    目录(更新于20150311) meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速 ...

  7. c# 利用反射动态给实体类对象赋值

    转:http://blog.sina.com.cn/s/blog_659a572b0100xp5s.html 例子如下 using System; using System.Collections.G ...

  8. 回调函数的应用误区4(c/s OK版本回调小程序)

    VC++深入详解里面说得也挺好:回调函数的实现机制: 1)定义一个回调函数 2)“函数实现者”(回调函数所在的模块)在初始化的时候,将回调函数的函数指针注册给“调用者”. 3)当特定的事件或条件发生的 ...

  9. centos使用网易163yum源

    CentOS系统自带的更新源的速度实在是慢,为了让CentOS6使用速度更快的YUM更新源,可以选择163(网易)的更新源. 1.下载repo文件 wget http://mirrors.163.co ...

  10. [Everyday Mathematics]20150110

    试证: $$\bex \vlm{n}\frac{\ln^2n}{n}\sum_{k=2}^{n-2}\frac{1}{\ln k\cdot \ln(n-k)}=1. \eex$$