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 Mapprivate static Map<String,Object> coffee2Value;static{coffee2Value = new LinkedHashMap<String,Object>();coffee2Value.put("Coffee2 - Cream Latte", "Cream Latte"); //label, valuecoffee2Value.put("Coffee2 - Extreme Mocha", "Extreme Mocha");coffee2Value.put("Coffee2 - Buena Vista", "Buena Vista");}public Map<String,Object> getFavCoffee2Value() {return coffee2Value;}//Generated by Object arraypublic 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.

转载于:https://www.cnblogs.com/ghgyj/p/4765389.html

JSF 2 dropdown box example相关推荐

  1. TNS service names ODBC dropdown box garbled, messed up, gibberish

    2011-09 本文仅记录下我解决ODBC 中向 TNS Service Name 下拉框添加数据库服务器选项的问题. Most likely cause is that there are two ...

  2. SAP Business Explorer 初探

    SAP Business Explorer 初探 Part I: 什么是SAP BEX ? SAP Business Explorer is a frontend tool to design Dat ...

  3. 在Mac OS X macOS Sierra上进行VirtualBox和GNS3链接

    YouTube上有一个添加Mac OS主机链接GNS3路由器的视频: https://www.youtube.com/watch?v=Cstk7CVLw0Y 网络上搜到的文档(英文): http:// ...

  4. 出色图形用户界面(GUI)设计规范

    来源:  作者:James Hobart 翻译:spark.bbs@bbs.nankai.edu.cn [字体:大 中 小] 出色图形用户界面(GUI)设计规范 作者:James Hobart 翻译: ...

  5. Python基础教程(十):CGI编程、MySQL数据库

    Python CGI编程 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如: ...

  6. textmate开发一个blog

    TextMate开发一个简单的Blog吧. 1.首先安装git,下载源码编译或者到这里下载package:[url]http://metastatic.org/text/Concern/2007/09 ...

  7. C运行库和C语言函数库/Microsoft C运行库

    C运行库和C语言函数库/Microsoft C运行库 2009-03-04 10:27 973人阅读 评论(0) 收藏 举报 c语言librarydll编译器fortran 1.C运行库和C语言函数库 ...

  8. Create your own blockchain amp; cryptocurrency! - understand blockchains by creating one in python

    What exactly is a blockchain? How does a blockchain work? What is Bitcoin or STEEM exactly? How does ...

  9. 机器学习特征构建_使用Streamlit构建您的基础机器学习Web应用

    机器学习特征构建 Data scientist and ML experts often find it difficult to showcase their findings/result to ...

  10. 谷歌云使用账号密码_如何使用Google密码检查

    谷歌云使用账号密码 Google has a tool designed to securely analyze your passwords against a database of ones t ...

最新文章

  1. python编程图_Python编程图形库之Pillow使用方法讲解
  2. 共创Linux防火墙,Linux防火墙iptables简明教程
  3. 利用SQLChiper对Android SQLite数据库加密
  4. ISP运营商实验室测试机架拓扑搭建经验分享
  5. Flutter State生命周期 Flutter Widget生命周期 Flutter 应用程序生命周期
  6. python def函数报错详解_python自定义函数def的应用详解
  7. 配置python开发环境搭建_Eclipse配置Python开发环境
  8. Python 连接开放航空交通数据,轻松构建航班跟踪应用!
  9. 高考分数出来了,计算机专业选择那个方向更香?
  10. 大数据分析在医疗行业的作用
  11. c语言编程编写笑脸,用C语言编写笑脸游戏.doc
  12. 孙玄达叔:年薪75万的真实技术面试实践攻略(篇章二)
  13. Windows10更新错误显示0x8000ffff,易升更新0xc1900107
  14. ​云队友丨两次疫情冲击,却两次成功上市,携程是怎么做到的?
  15. Win10下双系统Ubuntu14.04+GTX1070+CUDAcuDNN+Tensorflow环境搭建
  16. 丽台显卡测试软件,领先A卡62% 丽台7系显卡对比测试
  17. 苹果手机怎么把计算机放到桌面,iPhone怎么将电脑上的电影拖到手机上观看?
  18. 武汉大学计算机黄天成,UCL南区决赛武大强势夺得LOL冠军 韩国外援成焦点
  19. IMS 紧急服务相关概念
  20. 移动应用开发中AppID、AppKey、AppSecret到底是什么?

热门文章

  1. Bootstrap页面布局4 - 嵌套布局
  2. UVa232.Crossword Answers
  3. XCode下的iOS单元测试(转)
  4. IDEA创建maven项目之后无法编写java类
  5. P1019 单词接龙
  6. 使用DbFunctions来解决EF按照日期分组数据
  7. ASM:《X86汇编语言-从实模式到保护模式》第8章:实模式下硬盘的访问,程序重定位和加载...
  8. android中include标签使用详解
  9. 常见服务器返回的错误代码(转)
  10. jQuery图片轮播插件 jQuery Cycle Plugin