JSF selectManyListbox显示值绑定的错误

时间:2017-02-07 10:31:39

标签: java jsp jsf selectmanylistbox

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

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

statusItems 对象在以下bean类中定义:

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

我应该丢失什么或导致此问题?谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:-1)

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

此外,你应该考虑更换值=&#34;值&#34;来自

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

要:

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