PrimeFaces提供了一种基于过滤器创建动态下拉列表的简便方法。通过Ajax调用形成一个新的下拉列表。每个事件发生后, <
p:ajax>
标记将调用Java方法。
在下面的示例中, 我们将创建两个下拉菜单。本示例包括以下文件。
JSF文件
// ajax-dropdown.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://xmlns.jcp.org/jsf/html"xmlns:p="http://primefaces.org/ui"xmlns:f="http://xmlns.jcp.org/jsf/core">
<
h:head>
<
title>
Ajax Dropdown<
/title>
<
/h:head>
<
h:body>
<
h:form>
<
p:growl id="msgs" showDetail="true" />
<
p:panel header="Provide Your Address" style="margin-bottom:10px;
">
<
h:panelGrid columns="2" cellpadding="5">
<
p:outputLabel for="country" value="http://www.srcmini.com/Country:" />
<
p:selectOneMenu id="country" value="http://www.srcmini.com/#{dropdown.country}" style="width:150px">
<
p:ajax listener="#{dropdown.onCountryChange}" update="city" />
<
f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<
f:selectItems value="http://www.srcmini.com/#{dropdown.countries}" />
<
/p:selectOneMenu>
<
p:outputLabel for="city" value="http://www.srcmini.com/City:" />
<
p:selectOneMenu id="city" value="http://www.srcmini.com/#{dropdown.city}" style="width:150px">
<
f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
<
f:selectItems value="http://www.srcmini.com/#{dropdown.cities}" />
<
/p:selectOneMenu>
<
/h:panelGrid>
<
p:separator />
<
p:commandButton value="http://www.srcmini.com/Submit" update="msgs" actionListener="#{dropdown.displayLocation}" icon="ui-icon-check" />
<
/p:panel>
<
/h:form>
<
/h:body>
<
/html>
ManagedBean
// Dropdown.java
package com.srcmini;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean@ViewScopedpublic class Dropdown implements Serializable {private final Map<
String, Map<
String, String>
>
data = http://www.srcmini.com/new HashMap<
>
();
private String country;
private String city;
private Map<
String, String>
countries;
private Map<
String, String>
cities;
@PostConstructpublic void init() {countries= new HashMap<
>
();
countries.put("USA", "USA");
countries.put("India", "India");
countries.put("Russia", "Russia");
Map<
String, String>
map = new HashMap<
>
();
map.put("New York", "New York");
map.put("San Francisco", "San Francisco");
map.put("Denver", "Denver");
data.put("USA", map);
map = new HashMap<
>
();
map.put("New Delhi", "New Delhi");
map.put("Bombay", "Bombay");
map.put("Calcutta", "Calcutta");
data.put("India", map);
map = new HashMap<
>
();
map.put("Moscow", "Moscow");
map.put("Saint Petersburg", "Saint Petersburg");
map.put("Samara", "Samara");
data.put("Russia", map);
}public Map<
String, Map<
String, String>
>
getData() {return data;
}public String getCountry() {return country;
}public void setCountry(String country) {this.country = country;
}public String getCity() {return city;
}public void setCity(String city) {this.city = city;
}public Map<
String, String>
getCountries() {return countries;
}public Map<
String, String>
getCities() {return cities;
}public void onCountryChange() {if(country !=null &
&
!country.equals(""))cities = data.get(country);
elsecities = new HashMap<
>
();
}public void displayLocation() {FacesMessage msg;
if(city != null &
&
country != null)msg = new FacesMessage("Selected", city + " of " + country);
elsemsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid", "City is not selected.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}}package com.srcmini;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean@ViewScopedpublic class Dropdown implements Serializable {private final Map<
String, Map<
String, String>
>
data = http://www.srcmini.com/new HashMap<
>
();
private String country;
private String city;
private Map<
String, String>
countries;
private Map<
String, String>
cities;
@PostConstructpublic void init() {countries= new HashMap<
>
();
countries.put("USA", "USA");
countries.put("India", "India");
countries.put("Russia", "Russia");
Map<
String, String>
map = new HashMap<
>
();
map.put("New York", "New York");
map.put("San Francisco", "San Francisco");
map.put("Denver", "Denver");
data.put("USA", map);
map = new HashMap<
>
();
map.put("New Delhi", "New Delhi");
map.put("Bombay", "Bombay");
map.put("Calcutta", "Calcutta");
data.put("India", map);
map = new HashMap<
>
();
map.put("Moscow", "Moscow");
map.put("Saint Petersburg", "Saint Petersburg");
map.put("Samara", "Samara");
data.put("Russia", map);
}public Map<
String, Map<
String, String>
>
getData() {return data;
}public String getCountry() {return country;
}public void setCountry(String country) {this.country = country;
}public String getCity() {return city;
}public void setCity(String city) {this.city = city;
}public Map<
String, String>
getCountries() {return countries;
}public Map<
String, String>
getCities() {return cities;
}public void onCountryChange() {if(country !=null &
&
!country.equals(""))cities = data.get(country);
elsecities = new HashMap<
>
();
}public void displayLocation() {FacesMessage msg;
if(city != null &
&
country != null)msg = new FacesMessage("Selected", city + " of " + country);
elsemsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid", "City is not selected.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}}
执行后, 将产生以下输出。
输出
文章图片
【PrimeFaces Ajax下拉列表】第二个下拉列表包含每个国家/地区的城市名称。每次选择国家, 城市都会动态更新。
文章图片
文章图片
推荐阅读
- PrimeFaces Ajax事件示例
- PrimeFaces Ajax ActionListener示例
- PrimeFaces Ajax使用示例
- PrimeFaces AccordionPanel实例
- PowerShell Write-Host写入主机
- PowerShell Sort-Object对象排序
- PowerShell While循环语句
- PowerShell Where-Object使用例子
- 纯净版xp系统如何取得未运用IP地址|XP系统取得未运用IP地址的技巧