我有两个 h:selectOneMenu(1:国家,2:城市)。我需要使用 ajax 在城市 selectOneMenu 中加载来自一个选定国家的所有城市。当我更改国家/地区 selectOneMenu 的值时,我的城市 selectOneMenu 从 countryBean.selectedCountry 获取空值。
<h:panelGrid columns="2">
<h:outputLabel for="countries" value="Countries: " />
<h:selectOneMenu converter="omnifaces.SelectItemsConverter"
id="countries" required="true" value="#{countryBean.selectedCountry}">
<f:selectItem itemLabel="Choose country" />
<f:selectItems value="#{countriesBB.findAllCountries()}"
var="country" itemLabel="#{country.name}" />
<f:ajax event="change" render="cities" />
</h:selectOneMenu>
<h:outputLabel for="cities"
value="Cities: " />
<h:selectOneMenu converter="omnifaces.SelectItemsConverter"
id="cities" required="true"
value="#{cityBean.selectedCity}">
<f:selectItem itemLabel="Choose city" />
<f:selectItems value="#{cityBean.findAllCitiesByCountry(countryBean.selectedCountry)}"
var="city" itemLabel="#{city.name}" />
</h:selectOneMenu>
</h:panelGrid>
这是查找城市的方法:
public List<city> findAllCitiesByCountry(Country country) {
List<City> cities = null;
try {
cities = citiesService.findAllCitiesByCountry(country);
} catch (Exception exception) {
logger.debug("Error finding cities.", exception);
}
return cities;
}
我收到 NullPointerException 因为 countryBean.selectedCountry 始终为空。这样做的正确方法是什么?