JSF 2下拉框示例

在JSF中, <h:selectOneMenu />标记用于呈现一个下拉框-具有“ size = 1 ”属性的HTML select元素。

//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示例

一个JSF 2.0示例,展示了如何使用“ h:selectOneMenu ”标记来渲染下拉框,并以3种不同的方式填充数据:

  1. f:selectItem ”标签中的硬编码值。
  2. 使用Map生成值,并将其放入“ f:selectItems ”标签中。
  3. 使用Object数组生成值,并将其放入“ f:selectItems ”标记中,然后使用“ var ”属性表示该值。

1.菜豆

一个后备bean,用于保存并生成下拉框值的数据。

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页面

一个JSF页面,演示如何使用“ h:selectOneMenu ”标签。

<?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.演示

jsf2-dropdown-example-1

单击“提交”按钮时,链接到“ result.xhtml”页面并显示已提交的下拉框值。

f2-dropdown-example-2

如何预选下拉框值?

如果与“ h:selectOneMenu ”标签的“ value”匹配,则选择“ f:selectItems ”标签的值。 在上面的示例中,如果将“ favCoffee1”属性设置为“ Extreme Mocha”:

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

	public String favCoffee1 = "Extreme Mocha";

	//...

默认情况下,“ favCoffee1”下拉框的值为“ Extreme Mocha”。

下载源代码

下载它– JSF-2-Dropdown-Box-Example.zip (10KB)

参考
  1. JSF <h:selectOneMenu /> JavaDoc

翻译自: https://mkyong.com/jsf2/jsf-2-dropdown-box-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值