我有
j_idt7:city:验证错误:值无效
切入正题,ManagedBean 代码:
//few imports here
@ManagedBean
@SessionScoped
public class CountriesAndCities implements Serializable{
private List<SelectItem> countries;
private List<SelectItem> cities;
private Map<String,List> m;
private String selectedCountry;
public String getSelectedCountry() {
return selectedCountry;
}
public void setSelectedCountry(String selectedCountry) {
this.selectedCountry = selectedCountry;
}
public CountriesAndCities(){
countries = new ArrayList<SelectItem>();
cities = new ArrayList<SelectItem>();
m = new HashMap<String,List>();
m.put("France", Arrays.asList("paris","marseille"));
m.put("England", Arrays.asList("Munchester","liverpoor"));
}
public List<SelectItem> getCountries(){
cities.removeAll(cities);
countries.removeAll(countries);
countries.add(new SelectItem("select country"));
for(Map.Entry<String, List> entry: m.entrySet()){
countries.add(new SelectItem(entry.getKey()));
}
return countries;
}
public List<SelectItem> getCities(){
for(Map.Entry<String, List> entry: m.entrySet())
{if(entry.getKey().toString().equals(selectedCountry)){
cities.addAll(entry.getValue());
break;
}
}
return cities;
}
public void checkSelectedCountry(ValueChangeEvent event){
selectedCountry = event.getNewValue().toString();
}
这是我的 .xhtml 的片段:
<h:selectOneMenu immediate="true" value="#{countriesAndCities.selectedCountry}"
onchange="submit()" valueChangeListener="#{countriesAndCities.checkSelectedCountry}">
<f:selectItems value="#{countriesAndCities.countries}"></f:selectItems>
</h:selectOneMenu>
<br/>
<h:selectOneMenu id="city">
<f:selectItems value="#{countriesAndCities.cities}"></f:selectItems>
</h:selectOneMenu>
</h:form>
代码做了应该做的事情,但是我在第一行得到了上面提到的错误,只有当我点击英格兰并选择国家选项时,我不知道为什么,我在 Ajaxized 代码中编写了相同的任务,它工作得很好, 任何 手 都会 心存感激 .