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

//JSF...
<h:selectOneListbox value="#{user.favYear1}">
<f:selectItem itemValue="2000" itemLabel="Year1 - 2000" />
<f:selectItem itemValue="2010" itemLabel="Year1 - 2010" />
<f:selectItem itemValue="2020" itemLabel="Year1 - 2020" />
</h:selectOneListbox>
//HTML output...
<select name="j_idt6:j_idt8" size="3">
<option value="2000">Year1 - 2000</option>
<option value="2010">Year1 - 2010</option>
<option value="2020">Year1 - 2020</option>
</select>

h:selectOneListbox example

A JSF 2.0 example to show the use of “h:selectOneListbox” tag to render a single 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 listbox values.

package com.mkyong;

import java.io.Serializable;
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 favYear1;
public String favYear2;
public String favYear3; //getter and setter methods //Generated by Map
private static Map<String,Object> year2Value;
static{
year2Value = new LinkedHashMap<String,Object>();
year2Value.put("Year2 - 2000", "2000"); //label, value
year2Value.put("Year2 - 2010", "2010");
year2Value.put("Year2 - 2020", "2020");
} public Map<String,Object> getFavYear2Value() {
return year2Value;
} //Generated by Object array
public static class Year{
public String yearLabel;
public String yearValue; public Year(String yearLabel, String yearValue){
this.yearLabel = yearLabel;
this.yearValue = yearValue;
} public String getYearLabel(){
return yearLabel;
} public String getYearValue(){
return yearValue;
} } public Year[] year3List; public Year[] getFavYear3Value() { year3List = new Year[3];
year3List[0] = new Year("Year3 - 2000", "2000");
year3List[1] = new Year("Year3 - 2010", "2010");
year3List[2] = new Year("Year3 - 2020", "2020"); return year3List;
} }

2. JSF Page

A JSF page to demonstrate the use “h:selectOneListbox” 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 listbox example</h1>
<h:form> 1. Hard-coded with "f:selectItem" :
<h:selectOneListbox value="#{user.favYear1}">
<f:selectItem itemValue="2000" itemLabel="Year1 - 2000" />
<f:selectItem itemValue="2010" itemLabel="Year1 - 2010" />
<f:selectItem itemValue="2020" itemLabel="Year1 - 2020" />
</h:selectOneListbox> <br /> 2. Generated by Map :
<h:selectOneListbox value="#{user.favYear2}">
<f:selectItems value="#{user.favYear2Value}" />
</h:selectOneListbox> <br /> 3. Generated by Object array and iterate with var :
<h:selectOneListbox value="#{user.favYear3}">
<f:selectItems value="#{user.favYear3Value}" var="y"
itemLabel="#{y.yearLabel}" itemValue="#{y.yearValue}" />
</h:selectOneListbox> <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 listbox example</h1> <h2>result.xhtml</h2> <ol>
<li>user.favYear1 : #{user.favYear1}</li>
<li>user.favYear2 : #{user.favYear2}</li>
<li>user.favYear3 : #{user.favYear3}</li>
</ol>
</h:body> </html>

3. Demo



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

How to pre-select a listbox value?

The value of “f:selectItems” tag is selected if it matched to the “value” of “h:selectOneListbox” tag. In above example, if you set favYear1 property to “2010” :

@ManagedBean(name="user")
@SessionScoped
public class UserBean{ public String favYear1 = "2010"; //...

The “favYear1″ listbox, value “2010” is selected by default.

JSF 2 listbox example的更多相关文章

  1. JSF 2 multiple select listbox example

    In JSF, <h:selectManyListbox /> tag is used to render a multiple select listbox – HTML select ...

  2. JSF标签大全详解

    1. JSF入门 藉由以下的几个主题,可以大致了解JSF的轮廓与特性,我们来看看网页设计人员与应用程序设计人员各负责什么. 1.1简介JSF Web应用程序的开发与传统的单机程序开发在本质上存在着太多 ...

  3. MVVM下listbox默认显示最后一行

    原文地址:http://stackoverflow.com/questions/16866309/listbox-scroll-into-view-with-mvvm public class Scr ...

  4. WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板

    有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...

  5. JSF primefaces dataTable paginator 表格分页 问题

    当第一次查询返回list列表,分页1,2,3.....这是选择2,当前页面停留在第2页. 当再次查询后,因为使用的ajax,结果更新了,但当前页面依旧是第2页. 可以在jsf页面,datatable的 ...

  6. 关于JSF中immediate属性的总结(二)

    The immediate attribute in JSF is commonly misunderstood. If you don't believe me, check out Stack O ...

  7. WPF制作的VS黑色风格的Listbox

    最近写的一个玩具,WPF写出来的东西还是挺好看的 style.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/ ...

  8. jquery 双向select控件bootstrap Dual listbox

    http://www.cnblogs.com/hangwei/p/5040866.html       -->jquery 双向select控件bootstrap Dual listboxhtt ...

  9. jsf初学selectOneMenu 绑定与取值

    jsf 的selectOneMenu 最后生成的<select>标签.这里涉及到一个binding 起初一直不知道是干嘛的,后来参考了其他文章.就相当于在asp.net 中如:<as ...

随机推荐

  1. eclipse导入javax.servlet.*的方法

    1.下载web应用服务器tomact,网址http://tomcat.apache.org/download-80.cgi 这个根据自己系统进行选择. 2.将其加压到电脑中 3.在eclipse中添加 ...

  2. Oracle的rownum原理和使用(整理几个达人的帖子)

    整理和学习了一下网上高手关于rownum的帖子: 参考资料:  http://tech.ddvip.com/2008-10/122490439383296.html 和 http://tenn.jav ...

  3. 结构体TABLE_share

    struct TABLE_share { static inline TABLE **next_ptr(TABLE *l) { return &l->share_next; } stat ...

  4. bzoj3983

    显然我们得到这样几个结论 1.每次攻击对方一定是攻击最大的 2.自己合并也是合并最大和次大的 我们只要穷举下一开始是攻击还是合并,之后就是能攻击先攻击否则就合并 ..] of int64; var a ...

  5. ElasticSearch Remote Code Execution (CVE-2014-3120)

    Elasticsearch is a powerful open source search and analytics engine. The vulnerability allows attack ...

  6. poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)

    多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...

  7. codeforces 334B - Eight Point Sets

    题意难懂,其实就是x1<x2<x3与y1<y2<y3两两组合成九个点,去掉(x2,y2),剩余八个.这样的八个点才是满足要求的. 忘去重了 #include<cstdio ...

  8. I.MX6 Power off register hacking

    /*********************************************************************** * I.MX6 Power off register ...

  9. UVA 568 Just the Facts (水)

    题意: 求一个数n的阶乘,其往后数第1个不是0的数字是多少. 思路: [1,n]逐个乘,出现后缀0就过滤掉,比如12300就变成123,继续算下去.为解决爆long long问题,将其余一个数mod, ...

  10. Java [Leetcode 110]Balanced Binary Tree

    题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...