In JSF, <h:selectOneMenu /> tag is used to render a dropdown box – HTML select element with “size=1” attribute.

//JSF...
<h:selectOneMenu value="#{user.favCoffee1}">
<f:selectItem itemValue="Cream Latte" itemLabel="Coffee3 - Cream Latte" />
<f:selectItem itemValue="Extreme Mocha" itemLabel="Coffee3 - Extreme Mocha" />
<f:selectItem itemValue="Buena Vista" itemLabel="Coffee3 - Buena Vista" />
</h:selectOneMenu>
//HTML output...
<select name="j_idt6:j_idt8" size="1">
<option value="Cream Latte">Coffee3 - Cream Latte</option>
<option value="Extreme Mocha">Coffee3 - Extreme Mocha</option>
<option value="Buena Vista">Coffee3 - Buena Vista</option>
</select>

h:selectOneMenu example

A JSF 2.0 example to show the use of “h:selectOneMenu” tag to render a dropdow box, 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 dropdown box 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 favCoffee1;
public String favCoffee2;
public String favCoffee3; public String getFavCoffee1() {
return favCoffee1;
} public void setFavCoffee1(String favCoffee1) {
this.favCoffee1 = favCoffee1;
} public String getFavCoffee2() {
return favCoffee2;
} public void setFavCoffee2(String favCoffee2) {
this.favCoffee2 = favCoffee2;
} public String getFavCoffee3() {
return favCoffee3;
} public void setFavCoffee3(String favCoffee3) {
this.favCoffee3 = favCoffee3;
} //Generated by Map
private static Map<String,Object> coffee2Value;
static{
coffee2Value = new LinkedHashMap<String,Object>();
coffee2Value.put("Coffee2 - Cream Latte", "Cream Latte"); //label, value
coffee2Value.put("Coffee2 - Extreme Mocha", "Extreme Mocha");
coffee2Value.put("Coffee2 - Buena Vista", "Buena Vista");
} public Map<String,Object> getFavCoffee2Value() {
return coffee2Value;
} //Generated by Object array
public static class Coffee{
public String coffeeLabel;
public String coffeeValue; public Coffee(String coffeeLabel, String coffeeValue){
this.coffeeLabel = coffeeLabel;
this.coffeeValue = coffeeValue;
} public String getCoffeeLabel(){
return coffeeLabel;
} public String getCoffeeValue(){
return coffeeValue;
} } public Coffee[] coffee3List; public Coffee[] getFavCoffee3Value() { coffee3List = new Coffee[3];
coffee3List[0] = new Coffee("Coffee3 - Cream Latte", "Cream Latte");
coffee3List[1] = new Coffee("Coffee3 - Extreme Mocha", "Extreme Mocha");
coffee3List[2] = new Coffee("Coffee3 - Buena Vista", "Buena Vista"); return coffee3List; } }

2. JSF Page

A JSF page to demonstrate the use “h:selectOneMenu” 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 dropdown box example</h1>
<h:form> 1. Hard-coded with "f:selectItem" :
<h:selectOneMenu value="#{user.favCoffee1}">
<f:selectItem itemValue="Cream Latte" itemLabel="Coffee3 - Cream Latte" />
<f:selectItem itemValue="Extreme Mocha" itemLabel="Coffee3 - Extreme Mocha" />
<f:selectItem itemValue="Buena Vista" itemLabel="Coffee3 - Buena Vista" />
</h:selectOneMenu> <br /><br /> 2. Generated by Map :
<h:selectOneMenu value="#{user.favCoffee2}">
<f:selectItems value="#{user.favCoffee2Value}" />
</h:selectOneMenu> <br /><br /> 3. Generated by Object array and iterate with var :
<h:selectOneMenu value="#{user.favCoffee3}">
<f:selectItems value="#{user.favCoffee3Value}" var="c"
itemLabel="#{c.coffeeLabel}" itemValue="#{c.coffeeValue}" />
</h:selectOneMenu> <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 dropdown box example</h1> <h2>result.xhtml</h2> <ol>
<li>user.favCoffee1 : #{user.favCoffee1}</li>
<li>user.favCoffee2 : #{user.favCoffee2}</li>
<li>user.favCoffee3 : #{user.favCoffee3}</li>
</ol>
</h:body> </html>

3. Demo



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

How to pre-select a dropdown box value?

The value of “f:selectItems” tag is selected if it matched to the “value” of “h:selectOneMenu” tag. In above example, if you set “favCoffee1″ property to “Extreme Mocha” :

@ManagedBean(name="user")
@SessionScoped
public class UserBean{ public String favCoffee1 = "Extreme Mocha"; //...

The “favCoffee1″ dropdown box, values “Extreme Mocha” is selected by default.

JSF 2 dropdown box 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. [XAF] How to represent an enumeration property via a drop-down box with check boxes

    https://www.devexpress.com/Support/Center/Example/Details/E689

  3. 为WIN8 APP创建置顶desktop应用

    Windows 8: TopMost window   I am working on my next ambitious project “MouseTouch” which is multi to ...

  4. Azure HDInsight 和 Spark 大数据实战(二)

    HDInsight cluster on Linux 登录 Azure portal (https://manage.windowsazure.com ) 点击左下角的 NEW 按钮,然后点击 DAT ...

  5. Enable Cross-Origin Requests in Asp.Net WebApi 2[Reprint]

    Browser security prevents a web page from making AJAX requests to another domain. This restriction i ...

  6. <转>C Runtime Library(MSVCRT)来历

    (转载)C Runtime Library(MSVCRT)来历   msvcrt.dll (名称:Microsoft C Runtime Library)提供了printf,malloc,strcpy ...

  7. VC CComboBox用法总结

    VC每日一练,虽然简单,不动手试一下不能真正记住. 大气象 CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1); comboBox->I ...

  8. Enabling Cross-Origin Requests in ASP.NET Web API 2

    Introduction This tutorial demonstrates CORS support in ASP.NET Web API. We’ll start by creating two ...

  9. MessageBox Class

    Examples http://msdn.microsoft.com/en-us/library/aa969773(v=vs.110).aspx Displays a message box that ...

随机推荐

  1. 加密解密(9)Diffie-Hellman密钥交换协议

    过程如下 : 1,小李把KeyX经过加密变化,生成MsgA传给老王. 2,老王得到MsgA,保存在本地. 3,老王把KeyY经过加密变化,生成MsgB传给小李, 4,小李得到MsgB保存在本地, 5, ...

  2. 一些非常有用的html,css,javascript代码片段(持久更新)

    1.判断设备是否联网 if (navigator.onLine) { //some code }else{ //others code } 2.获取url的指定参数 function getStrin ...

  3. BZOJ 1923 外星千足虫(高斯消元)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1923 题意:有n个数字,m次测试.每个数字为0或者1.每次测试选出一些数字出来把他们加起 ...

  4. WPF中的Drawing

    以前在用WinForm的时候,可以通过GDI+接口在窗体上动态绘制自定义的图形.在WPF中有没有对应的API呢,最近项目中用到了这个,在这里总结一下. WPF中的Drawing主要提供了几类API: ...

  5. Hibernate4.2.2使用Annotation配置

    1.在hibernate官网下载hibernate-release-4.2.2.Final.zip并解压 2.新建一个java project工程(20130619_Hibernate4.2.2_An ...

  6. mysql 分页存储过程 一次返回两个记录集(行的条数,以及行记录),DataReader的Read方法和NextResult方法

    DELIMITER $$ USE `netschool`$$ DROP PROCEDURE IF EXISTS `fn_jk_GetCourses`$$ CREATE DEFINER=`root`@` ...

  7. [反汇编练习] 160个CrackMe之014

    [反汇编练习] 160个CrackMe之014. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  8. 纯css3写的仿真图书翻页效果

    对css3研究越深入,越觉得惊艳.css3说不上是万能的,但是它能实现的效果也超出了我的想象.它的高效率和动画效果的流畅性很多情况下能替代js的作用.个人习惯css3能实现的效果就不会用js,虽然在国 ...

  9. POJ 1146 ID Codes (UVA146)

    // 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...

  10. 【JSP】<meta>标签用法

    转载自:http://blog.sina.com.cn/s/blog_65c74cce0102v39z.html  非常感谢这位博主,急着用,改日再细细品味重新整理这篇博文. http-equiv M ...