JSF selectManyListbox 显示值绑定错误

JSF selectManyListbox shows error of value binding

我在 JSP 中使用 JSF 标签 h:selectManyListbox 来显示 bean 中的项目列表。

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

statusItems 对象在以下 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;
   }
}

但是我在加载这个时收到一条错误消息:


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

我应该缺少什么或导致这个问题?感谢您的帮助

您的绑定不太正确。在这种情况下,您需要使用示例中的 Collection 或 Array: https://www.tutorialspoint.com/jsf/jsf_selectmanylistbox_tag.htm

此外,您应该考虑从

中替换 value="value" 属性
<f:selectItem value="#{settingsBean.statusItems}" />

收件人:

<f:selectItem itemValue="#{settingsBean.statusItems}" />

在 JSF 中,我们有两个不同的标签 selectItemselectItemsselectItem 用于显示单个项目,尽管我们可以使用多个 selectItem 标签来显示多个值。但是如果我们有一个 selectItems 的列表,那么我们应该使用 selectItems 而不是 selectItem。因此,将 XHTML 上的 selectItem 标记替换为 selectItems,如下所示:

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