如果没有submit() JSF,值更改侦听器将无法工作



这是我的JSP:

  <h:panelGrid columns="2">
Selected Location : 
    <h:inputText id="emp" value="#{employee.locationCode}" size="20" />
    Select a country {method binding}: 
    <h:selectOneMenu id="empl" value="#{employee.locationCode}" 
        valueChangeListener="#{employee.countryLocationCodeChanged}" onchange="submit()">
        <f:selectItems value="#{employee.locationInMap}" />
    </h:selectOneMenu>
  </h:panelGrid>

我的豆:私有静态Map;private String locationCode = "en";//默认值

static{
    locationMap = new LinkedHashMap<String,String>();
    locationMap.put("United Kingdom", "en"); //label, value
    locationMap.put("French", "fr");
    locationMap.put("German", "de");
    locationMap.put("China", "zh_CN");
    }
public void countryLocationCodeChanged(ValueChangeEvent e){
    //assign new value to localeCode
    setLocationCode(e.getNewValue().toString());
}
public Map<String,String> getLocationInMap() {
    return this.locationMap;
}
public String getLocationCode() {
    return locationCode;
}
public void setLocationCode(String locationCode) {
    this.locationCode = locationCode;
}
public void changeEvent(String locationCode) {
    this.locationCode = locationCode;
}

除此之外,我在jsp中有很多更多的字段。当我选择下拉值时,它会给出非法参数异常。我猜submit()产生了一些问题…有人能帮忙吗?提前感谢……

问题出在你的selectItems标签上。

<f:selectItems value="#{employee.locationInMap}" />

你不能初始化整个映射到selectOneMenu标签。

直接输入entryset:

<f:selectItems value="#{bean.locationInMap.entrySet()}" var="entry" 
itemValue="#{entry.key}" itemLabel="#{entry.value}" />

你可以在这里找到更多关于selectOneMenu的信息

最新更新