JSF 2 dropdown box example
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的更多相关文章
- JSF 2 multiple select dropdown box example
In JSF, <h:selectManyMenu /> tag is used to render a multiple select dropdown box – HTML selec ...
- [XAF] How to represent an enumeration property via a drop-down box with check boxes
https://www.devexpress.com/Support/Center/Example/Details/E689
- 为WIN8 APP创建置顶desktop应用
Windows 8: TopMost window I am working on my next ambitious project “MouseTouch” which is multi to ...
- Azure HDInsight 和 Spark 大数据实战(二)
HDInsight cluster on Linux 登录 Azure portal (https://manage.windowsazure.com ) 点击左下角的 NEW 按钮,然后点击 DAT ...
- 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 ...
- <转>C Runtime Library(MSVCRT)来历
(转载)C Runtime Library(MSVCRT)来历 msvcrt.dll (名称:Microsoft C Runtime Library)提供了printf,malloc,strcpy ...
- VC CComboBox用法总结
VC每日一练,虽然简单,不动手试一下不能真正记住. 大气象 CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1); comboBox->I ...
- 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 ...
- MessageBox Class
Examples http://msdn.microsoft.com/en-us/library/aa969773(v=vs.110).aspx Displays a message box that ...
随机推荐
- Android权限安全(4)在什么时候检验权限?
Android独有的Service等 : 通过PM的CheckPermission 其中 pm 是package manager services 非Android特有的Service等 : 映射为O ...
- BZOJ 2326 数学作业(矩阵)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2326 题意:定义Concatenate(1,N)=1234567……n.比如Concat ...
- CRC校验码
循环冗余校验码(CRC)的基本原理是:在K位信息码后再拼接R位的校验码,整个编码长度为N位,因此,这种编码也叫(N,K)码.对于一个给定的(N,K)码,可以证明存在一个最高次幂为R的多项式G(x)(R ...
- JAVA将Excel中的报表导出为图片格式(三)换一种实现
上一篇介绍了使用Java的Robot机器人实现截图,然后将剪贴板上的数据流生成PNG图片 但是经过博主的不断测试,在完全依赖远程桌面的没有终端显示器的服务器上 使用截图方式是不可行的,因为一旦使用了远 ...
- iOS开发:记录开发中遇到的编译或运行异常以及解决方案
1.部署到真机异常 dyld`dyld_fatal_error: -> 0x120015088 <+0>: brk #0x3 dyld: Library not loaded ...
- Parallel并行运算实例
并行运算Parallel,是.net 4.0版本里添加的新处理方式,主要充分利用CPU.任务并发的模式来达到提高运算能力.简单理解为每个CPU都在处理任务,而不会让它们空闲下来. 直接看实例: nam ...
- UVa 10868 (物理) Bungee Jumping
题意: 有个人在蹦极,给出悬崖的高度,绳子的长度,弹簧绳的胡克系数 以及 人的质量. 判断人是否能够着地,能的话是否能安全着地.所谓安全着地就是到达地面的速度不超过10m/s. 分析: 学过一点高中物 ...
- ORACLE CLIENT客户端安装步骤详解
下载地址: http://download.oracle.com/otn/nt/oracle11g/112010/win32_11gR2_client.zip 先将下载下来的ZIP文件解压,并运行se ...
- $( ).focus()与$( )[0].focus()区别
$( #id).focus()与$( #id)[0].focus()没有区别,因为id必须是唯一的.如果同一页面出现多个相同的ID(这是不符合w3c规范的),$(#id)也只会拿到第一个该ID,后面的 ...
- .Net刷新页面的小结
现在给大家讲讲在.Net中书信页面的几种方式: 第一: private void Button1_Click( object sender, System.EventArgs e ) { Respon ...