Search code examples
javajspjsfselectmanylistbox

JSF selectManyListbox shows error of value binding


I am using JSF tag h:selectManyListbox in JSP to display a list of items from a bean.

<h:selectManyListbox value="#{settingsBean.statusIds}" style="width: 100%; height: 200px;">
    <f:selectItem value="#{settingsBean.statusItems}" />
</h:selectManyListbox>

The statusItems object is defined in the following bean class:

SettingsBean.java

public class SettingsBean {
   private List<String> statusIds;
   private List<SelectItem> statusItems;

   public SettingsBean() {
       initStatus();
   }    

   private void initStatus() {
       statusItems = new ArrayList<SelectItem>();

       statusItems.add(new SelectItem("v1", "lbl1"));
       statusItems.add(new SelectItem("v2", "lbl2"));
       statusItems.add(new SelectItem("v3", "lbl3"));
   }

   public ArrayList getStatusItems(){
       return getStatusItemsList(false);
   }

   @SuppressWarnings("unchecked")
   private ArrayList getStatusItemsList(boolean selected) {
       ArrayList ids = new ArrayList();     
       if (!selected) {
           boolean inSelIds = false;
           for (int i=0; i < statusItems.size(); i++) {
               inSelIds = false;
               SelectItem item = (SelectItem)statusItems.get(i);

               if (selected==inSelIds) {
                   String text = item.getLabel();                   
                   //ids.add(text);
                   ids.add(new SelectItem(item.getValue(), text));
               }
           }
       }

       return ids;
   }
}

But I am getting an error message when loading this:


HTTP Status 500 - java.lang.IllegalArgumentException: Value binding '#{settingsBean.statusItems}' of UISelectItem : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /jsp/Settings.jsp][Class: javax.faces.component.html.HtmlSelectManyListbox,Id: _id3][Class: javax.faces.component.UISelectItem,Id: _id4]} does not reference an Object of type SelectItem

What should I be missing or causing this problem? Thank you for your help


Solution

  • In JSF we have two different tags selectItem and selectItems. selectItem is used to display single item, although we can use multiple selectItem tags to show multiple values. But if we have a list of selectItems then we should use selectItems rather than selectItem. So replace you selectItem tag on your XHTML with the selectItems like below:

    <h:selectManyListbox value="#{settingsBean.statusIds}" style="width: 100%; height: 200px;">
        <f:selectItems value="#{settingsBean.statusItems}" />
    </h:selectManyListbox>