【问题标题】:Unable to retrieve arraylist elements on icefaces(jsf) xhtml page无法在 icefaces(jsf) xhtml 页面上检索 arraylist 元素
【发布时间】:2012-04-01 03:51:50
【问题描述】:

我的页面上有HTML 表,并试图用我的托管bean 中的一些数据填充它,我的xhtml 页面看起来像:

       <ice:panelGrid columns="2">
            <ice:panelGrid>
                <ice:outputText value="Properties:" style="text-align:left;font-size:20px;"></ice:outputText>
                <ice:selectManyListbox id="CriteriaListbox" style="width: 200px; height: 250px; " partialSubmit="true">
                 <p:selectItem value="#{beanInfo.properties}"/>
                </ice:selectManyListbox>
            </ice:panelGrid>
      </ice:panelGrid>

我的托管 bean 看起来像:

public ArrayList<String> getProperties()
{
    return properties;
}

并在构造函数中填充properties,如图所示:

public BeanInfo(){
   createProperties();
}

createProperties(){
    ArrayList<String> properties = new ArrayList<String>();
    properties.add("roi");
    properties.add("val");
}

我是 jsficefaces 的新手,所以不确定这里有什么问题。有什么建议吗?

更新

所以我的表中没有任何内容,但我得到java.util.ArrayList cannot be cast to javaax.faces.model.SelectItem 异常。

更新 2

这是我在Nikita's Approach 之后遇到的例外情况,并将我的JSF 版本从Mojarra-2.0.3 更新为Mojarra-2.1.7,任何建议。

Error Rendering View[/admin/Template.xhtml]: java.lang.ClassCastException: java.lang.String cannot be cast to javax.faces.model.SelectItem
    at com.icesoft.faces.renderkit.dom_html_basic.MenuRenderer.countSelectOptionsRecursive(MenuRenderer.java:440) [:]
    at com.icesoft.faces.renderkit.dom_html_basic.MenuRenderer.renderSelect(MenuRenderer.java:366) [:]
    at com.icesoft.faces.renderkit.dom_html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:108) [:]
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) [:2.1.7-SNAPSHOT]
    at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:359) [:]
    at com.icesoft.faces.renderkit.dom_html_basic.GridRenderer.encodeChildren(GridRenderer.java:197) [:]
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) [:2.1.7-SNAPSHOT]
    at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:347) [:]
    at com.icesoft.faces.renderkit.dom_html_basic.GridRenderer.encodeChildren(GridRenderer.java:197) [:]
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) [:2.1.7-SNAPSHOT]
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779) [:2.1.7-SNAPSHOT]
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782) [:2.1.7-SNAPSHOT]
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782) [:2.1.7-SNAPSHOT]
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782) [:2.1.7-SNAPSHOT]
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) [:2.1.7-SNAPSHOT]
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125) [:2.1.7-SNAPSHOT]
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [:2.1.7-SNAPSHOT]
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [:2.1.7-SNAPSHOT]
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [:2.1.7-SNAPSHOT]
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) [:2.1.7-SNAPSHOT]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]

更新 3:当前 xhtml

  <ice:panelGrid columns="2">
            <ice:panelGrid>
                <ice:outputText value="Properties:" style="text-align:left;font-size:20px;"></ice:outputText>
                <ice:selectManyListbox id="CriteriaListbox" style="width: 200px; height: 250px; " partialSubmit="true">
                 <p:selectItems value="#{bookBeanInfo.properties}"
                                  var="property"
                                  itemValue="#{property}"
                                  itemLabel="#{property}"/>

                </ice:selectManyListbox>

            </ice:panelGrid>

            <ice:panelGrid>
                <ice:outputText value="Name:" style="text-align:left;font-size:20px;" id="bookName"></ice:outputText>
            </ice:panelGrid>
            <ice:panelGrid>
                <ice:inputText id="NameInputText" style="width: 195px;" value="#{bookBeanInfo.bookName}"></ice:inputText>
            </ice:panelGrid>

更新 4:命名空间声明

html xmlns="http://www.w3.org/1999/xhtml"
xmlns:icecore="http://www.icefaces.org/icefaces/core"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ace="http://www.icefaces.org/icefaces/components"
xmlns:p="http://java.sun.com/jsf/core"
xmlns:ice-cc="http://www.icesoft.com/icefaces-composite-comps">

更新5

我能够通过使用 SelectItem 类型的数组列表而不是 String 来修复异常,所以在我的 bean 中,我有:

createProperties(){
    ArrayList<SelectItem> properties = new ArrayList<SelectItem>();
    properties.add(new SelectItem("roi", "roi"));
    properties.add(new SelectItem("val"."val"));
}

在我的xhtml 页面中,我必须使用selectItems 而不是selectItem,因为在我的xhtml 页面上期待收集,因此需要使用selectItems 来遍历它们:

 <ice:panelGrid columns="2">
        <ice:panelGrid>
            <ice:outputText value="Properties:" style="text-align:left;font-size:20px;"></ice:outputText>
            <ice:selectManyListbox id="CriteriaListbox" style="width: 200px; height: 250px; " partialSubmit="true">
             <p:selectItems value="#{beanInfo.properties}"/>
            </ice:selectManyListbox>
        </ice:panelGrid>
  </ice:panelGrid>

【问题讨论】:

  • 你能告诉你当前的 xhtml 吗?
  • 什么是p:selectItems?真的是JSF标准selectItems吗?你能显示p命名空间的声明吗?
  • @NikitaBeloglazov:更新了命名空间声明的问题
  • @NikitaBeloglazov:这真的很奇怪,不是吗?
  • 好的。然后你能说你的icefaces lib版本吗? :)

标签: java jsf icefaces


【解决方案1】:

它导致 ClasscastException,因为在 bean 的构造函数中,您正在创建 String 类型的集合,即Arraylist&lt;String&gt;,而 JSF 使用 SelectItem 类型的集合,即Arraylist&lt;SelectItems&gt;。当前设置页面渲染时抛出 ClasscastException,这是显而易见的。

可能的修复: (1) 在构造函数中改变集合的类型。做了 Arraylist&lt;SelectItem&gt; (2) &lt;f:selectItem&gt; (正如其他人所建议的那样)应该可以工作。但如果没有,请尝试以下操作:

 <ice:selectOneMenu value="myProperties">  
   <ice:selectItems value="#{beanInfo.properties}" />
 </ice:selectOneMenu>

【讨论】:

  • 这里提到的myProperties是什么?
  • 也是JSF默认使用ArrayList&lt;SelectItem&gt;类型的集合吗?
  • @Rachel : myProperties 只是一个文本,它将作为标签显示在您的下拉列表中。
  • 我不需要下拉菜单,我需要有一个列表框,其中列出了从 managedbean 返回的 arraylist 的所有元素
  • @Rachel:让 listBox 只需更改组件名称。因此,在您的 XHTML 中,您将使用类似 &lt;ice:selectOneListbox&gt; 的东西来代替 &lt;ice:selectOneMenu&gt;
【解决方案2】:

您应该在 getter 中使用 selectitem 而不是字符串

public ArrayList<SelectItem> getProperties() {
   return properties;
}

并用选择项填充您的属性

properties.add(new SelectItem(<the value>, <text to display>));

【讨论】:

  • 您应该使用 同时将命名空间更改为 f 和 p xmlns:f="java.sun.com/jsf/core 的标准使用”。然后它变成
  • 这不起作用并抛出classcastexception mentioned in update2
  • 你也改成了arraylist
【解决方案3】:

你为什么使用命名空间p 来代替&lt;p:selectItem value="#{beanInfo.properties}"/&gt;p 通常是 primefaces 组件,我不确定混合组件库是一种好习惯。试试jsf的标准&lt;f:selectItems value="#{beanInfo.properties"/&gt;
请注意,当您使用列表作为值时,您应该使用 selectItems 而不是 selectItem。

更新
试试

<f:selectItems value="#{beanInfo.properties}"
               var="property"
               itemValue="#{property}"
               itemLabel="#{property}"/>

【讨论】:

  • 您是否尝试过 selectItems,而不是 selectItem?
  • 你为什么使用 var 作为property
  • 我使用它是因为 jsf(或 icefaces)需要获取 SelectItem 对象的集合。您可以按照@roel 的建议在value 属性中传递它们的集合,也可以传递普通Java 对象的集合并使用varitemValueitemLabel 通过jsf 创建这些SelectItems
  • 所以你也可以拥有属性或测试来代替 var 的属性,对吧?
  • 另外,我不知道为什么会收到java.util.ArrayList cannot be cast to javaax.faces.model.SelectItem,你以前遇到过这个问题吗?
猜你喜欢
  • 1970-01-01
  • 2021-08-21
  • 2015-11-25
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2018-02-04
  • 1970-01-01
相关资源
最近更新 更多