我执行两个SelectOneMenu控件时遇到麻烦,其中第二个选择的数据取决于第一个选择。在PrimeFaces展示的这个例子与我想要实现的几乎相同:
http://www.primefaces.org/showcase-labs/ui/pprSelect.jsf
上述示例在同一个项目中正常工作。我正在使用NetBeans 7.0与GlassFish 3.1和PrimeFaces 3.0.M2,最新的下降(2011年6月20日)。
<?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:p="http://primefaces.prime.com.tr/ui" xmlns:f="http://java.sun.com/jsf/core"> <h:head><title>Facelet Title</title></h:head> <h:body> <p:log /> <center> <h:form> <h:outputText value="State: "/> <p:selectOneMenu id="selectState" value="#{stateCityBean.selectedStateArray}"> <f:selectItem itemLabel="Select Any" itemValue="Empty String"/> <p:ajax update="selectCity" listener="#{stateCityBean.updateCityMap}"/> <f:selectItems value="#{stateCityBean.stateMap}" /> </p:selectOneMenu> <p></p> <h:outputText value="City: "/> <p:selectOneMenu id="selectCity" value="#{stateCityBean.selectedCityArray}"> <f:selectItem itemLabel="Select Any" itemValue="Empty String"/> <f:selectItems value="#{stateCityBean.cityMap}"/> </p:selectOneMenu> </h:form> </center> </h:body>
StateCityBean.java
package com.xyz.mbeans; import com.iwizability.priceinfo.dao.*; import com.iwizability.priceinfo.pojo.*; import java.util.LinkedHashMap; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.faces.context.Flash; import javax.faces.event.ValueChangeEvent; @ManagedBean @SessionScoped public class StateCityBean { private String selectedStateArray; private Map<String,State> StateMap; private Map<String,City> CityMap; private String selectedCityArray; public StateCityBean() { System.out.println("Inside.............. "); StateMap = new LinkedHashMap<String,State>(); CityMap = new LinkedHashMap<String,City>(); } public String getSelectedStateArray() {return selectedStateArray;} public void setSelectedStateArray(String selectedStateArray) {this.selectedStateArray = selectedStateArray;} public Map<String,State> getStateMap() { StateDaoImpl stateObj = new StateDaoImpl(); StateMap = stateObj.getState(); return StateMap; } public void setStateMap(Map<String,State> stateArray) {this.StateMap = stateArray;} public String getSelectedCityArray() {return selectedCityArray;} public void setSelectedCityArray(String selectedCityArray) {this.selectedCityArray = selectedCityArray;} public Map<String,City> getCityMap() { CityDaoImpl cityObj = new CityDaoImpl(); int stateId = 0; if (selectedStateArray != null && !selectedStateArray.equals("")) { stateId = StateMap.get(selectedStateArray).getId(); } CityMap = cityObj.getCity(stateId); return CityMap; } public void setCityMap(Map<String,City> CityArray) { this.CityMap = CityArray; } public void updateCityMap() { CityDaoImpl cityObj = new CityDaoImpl(); int stateId = 0; if (selectedStateArray != null && !selectedStateArray.equals("")) { stateId = StateMap.get(selectedStateArray).getId(); this.CityMap = cityObj.getCity(stateId); } }
}
调试时,我可以看到updateCityMap方法被调用,但是SelectedStateArray变量是空的。即使强制更改绑定的CityMap变量的值也不会更新selectCity下拉列表。
正如你所猜到的,我是JSF的新手,但是由于我正在使用标签库的开发版本,所以问题更加复杂
我为您在项目中描述的完全相同的情况创建了一个演示。我有一个州和城市< p:selectOneMenu />我的页面上的元素您选择一个州,并更新城市。如果选择了不同的状态,城市将被删除,因为该状态可能不存在。
原文链接:https://www.f2er.com/ajax/160326.html区别在于我使用< p:ajax event =“change”update =“cities,cs”/>更新元素,以及一个actionListener来更新城市,如果状态不同。
<p:selectOneMenu id="states" value="#{dataBean.selectedState}" valueChangeListener="#{dataBean.stateChangeListener(event)}" style="width: 150px;"> <f:selectItem itemLabel="" itemValue=""/> <f:selectItems value="#{dataBean.states}"/> <p:ajax event="change" update="cities,cs"/> </p:selectOneMenu> <h:outputLabel value="City:" for="cities"/> <p:selectOneMenu id="cities" value="#{dataBean.selectedCity}" style="width: 150px;"> <f:selectItem itemLabel="" itemValue=""/> <f:selectItems value="#{dataBean.cities}"/> <p:ajax event="change" update="cs" /> </p:selectOneMenu>
整个项目和演示代码可以在我的博客上找到。我看到这个帖子,决定发布我的项目。 [博客]:http://javaevangelist.blogspot.com/2012/07/primefaces-ajax-enabled.html