1

I would like to show the element of the HashMap in a xhtml page (with jsf):

  <h:selectOneMenu id="elt" value="#{mgbean.elt}">
                       <c:forEach var="entry" items="#{mgbean.map}">
                            <f:selectItem itemValue="#{entry.key}" itemLabel="#{entry.value}" escape="false" />                            
                       </c:forEach>
     </h:selectOneMenu>

the Map is the following:

map= new LinkedHashMap<String, String>();
                map.put("1", ">=20");
                map.put("2","<20");

I got this error :

PM org.apache.myfaces.lifecycle.RenderResponseExecutor execute WARNING: There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered. These unhandled FacesMessages are: - selectionForm:elt: Validation Error: Value is not valid

i tried to add the following lines but the same error showed

<?xml version="1.0" encoding="UTF-8"?>  

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>

how can i fix this error.

UPDATE:

public String elt; with getter and setter.

public Map<String, String> map;


map= new LinkedHashMap<String, String>();
            map.put("1", ">=20");
            map.put("2", "<20");
Selma BA
  • 149
  • 3
  • 18

1 Answers1

0

I think you should opt for f:selectItem, it will take the key as the label and value as.. value:

<h:selectOneMenu id="elt" value="#{mgbean.elt}">
    <f:selectItems value="#{mgbean.map}"/>                            
</h:selectOneMenu>

Update

If you want to manually define whether key or value should be the label then try:

<h:selectOneMenu id="elt" value="#{mgbean.elt}">
     <f:selectItems value="#{mgbean.map.entrySet()}"
         var="entry" itemValue="#{entry.value}" itemLabel="#{entry.key}"/>                            
</h:selectOneMenu>

Also remember that at any given point in time the value contained in the mgbean.elt field must always be one of the keys of your Map. If for some reason you set that value manually to something different that "1" or "2", you might get error as above.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63