Reputation: 590
Here is my 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>
My bean: private static Map locationMap; private String locationCode = "en"; //default value
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;
}
Apart form this i have lot more fields in jsp. Its giving illegal argument exception when i am selecting drop down values. I guess submit() is making some issues.. can anybody help. Thanks in advance..
Upvotes: 1
Views: 1113
Reputation: 464
The problem is with your selectItems tag.
<f:selectItems value="#{employee.locationInMap}" />
You cann't intialize entire map to selectOneMenu tag.
just try with entryset as below:
<f:selectItems value="#{bean.locationInMap.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.value}" />
You can find more information on selectOneMenu here
Upvotes: 2