JSF 2 multiple select listbox example
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
Mapand put it into “f:selectItems” tag. - Generate values with an
Objectarray 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的更多相关文章
- JSF 2 multiple select dropdown box example
In JSF, <h:selectManyMenu /> tag is used to render a multiple select dropdown box – HTML selec ...
- 目录窗口多选Multiple Select in Catalog Window or arccatalog
目录窗口多选Multiple Select in Catalog Window or arccatalog 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#q ...
- html multiple select option 分组
普通html方式展示<select name="viewType" style="width: 100%;height: 300px;" multiple ...
- JSF 2 listbox example
In JSF, <h:selectOneListbox /> tag is used to render a single select listbox – HTML select ele ...
- ListBox控件例子
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.c ...
- Web页面中两个listbox的option的转移
Html: <div><span>所选时间:</span><select id="xuanyongTimelb" style=" ...
- 前端插件之Bootstrap Dual Listbox使用
工欲善其事,必先利其器 对于很多非专业前端开发来说写页面是非常痛苦的,借助框架或插件往往能够达到事半功倍的效果,本系列文章会介绍我在运维系统开发过程中用到的那些顺手的前端插件,如果你是想写XX管理系统 ...
- select框宽度与高度设置(实用版)
在IE中只能使用 font-size: 限制 select 的高度. 同时使用 width:200px 限制宽度 size="20" 表示最多显示20个选项,超过20的需要 ...
- js select级联,上面分类,下面是内容
js select级联,上面分类,下面是内容. js级联效果如下: 分类: 请选择 水果 蔬菜 其他 内容: // html和js代码如下: <html> <hea ...
随机推荐
- BZOJ 2154 Crash的数字表格
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2154 题意: 思路: i64 mou[N]; void init(int N){ ...
- Android Studio安装后第一次进不去
Android Studio 安装后第一次进不去,因为检查到有更新的SDK,在下载.但是呢,没有FQ的情况下,无法下载下来,所以就卡住了. 那么解决方案就是让 Android Studio 第一次启动 ...
- [HDOJ2874]Connections between cities(LCA, 离线tarjan)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 这题有不连通的情况,特别注意. 觉得是存query的姿势不对,用前向星存了一遍,还是T…… /* ...
- [HDOJ2795]Billboard(线段树,单点更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:w*h的公告板要贴公告,公告是w*1的,每个公告有先后顺序,要使每个公告贴的位置尽可能地高 ...
- BZOJ 1000: A+B Problem
问题:A + B问题 描述:http://acm.wust.edu.cn/problem.php?id=1000&soj=0 代码示例: import java.util.Scanner; p ...
- drop.delete.trauncat的区别
delete删除数据,保留表结构,可以回滚,如果数据量大,很慢,回滚是因为备份了删除的数据(删除数据时有两个动作,删除和备份) truncate删除所有数据,保留表结构,不可以回滚,一次全部删除所有数 ...
- Android开发时提示Your project contains error(s),please fix them be
有次在使用eclipse写好Android的代码,代码没有报错.然后 想在AVD中运行测试时,总是会弹出错误框,提示信息为: “Your project contains error(s),pl ...
- (转)Linux: su sudo sudoer
http://zebralinux.blog.51cto.com/8627088/1369301 日常操作中为了避免一些误操作,更加安全的管理系统,通常使用的用户身份都为普通用户,而非root.当需要 ...
- 六款最佳Linux教育应用
导读 对教育行业的用户来说,有好几款专门的Linux发行版是专门面向教育行业的.本文将介绍适合教育领域的几款顶级发行版. 1.Edubuntu 位居榜首的是Edubuntu.顾名思义,Edubuntu ...
- C++实现网格水印之调试笔记(一)
首先说一下我的一些简单的调试方法,除了常规的断点调试之外,我还会使用注释的方法来调试.当整个工程代码量相当多且调用层次关系较为复杂时,这种方法能够比较高效的定位到出错误的代码段或某个函数,然后在出现错 ...