xhtml页面上检索arraylist元素

xhtml页面上检索arraylist元素

本文介绍了无法在icefaces(jsf)xhtml页面上检索arraylist元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having HTML table on my page and am trying to populate it with some data from my managed bean, my xhtml page looks like:

       <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如下:

My managed bean looks like:

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

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

and in constructor am populating properties as shown:

public BeanInfo(){
   createProperties();
}

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

jsficefaces的新手,因此不确定此处的问题是什么.有什么建议吗?

Am new to jsf and icefaces and so not sure what is the issue in here. Any suggestions?

更新

因此,我的表中什么都没有,但出现了java.util.ArrayList cannot be cast to javaax.faces.model.SelectItem异常.

So there is nothing in my table but am getting java.util.ArrayList cannot be cast to javaax.faces.model.SelectItem exception.

更新2

这是在 Nikita的方法之后,并且将我的JSF版本从Mojarra-2.0.3更新为,任何建议.

This is the exception am getting after Nikita's Approach and updating my JSF version from Mojarra-2.0.3 to Mojarra-2.1.7, any suggestions.

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中,我有:

I was able to fix the exception by using array list of SelectItem types rather then String so in my bean, I have:

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

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

and in my xhtml page, i have to use selectItems instead of selectItem as on my xhtml page am expecting collection and so need to use selectItems to iterate through them:

 <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>

推荐答案

它引起ClasscastException,因为在bean的构造函数中,您正在收集String类型的集合,即Arraylist<String>,而JSF使用了SelectItem类型的集合,即Arraylist<SelectItems>.当使用当前设置页面呈现时,它会发出ClasscastException,这很明显.

It is causing ClasscastException because in constructor of bean you are making a collection of type String i.e. Arraylist<String> while JSF uses collection of type SelectItem i.e. Arraylist<SelectItems>. When with current setting page renders it thows ClasscastException, which s obvious.

可能的解决方法: (1)更改构造函数中的集合类型.做了 Arraylist<SelectItem> (2) <f:selectItem>(由其他人建议)应该可以工作.但是,如果没有,请尝试以下操作:

Posible Fix: (1) change the type of collection in constructor. Make it Arraylist<SelectItem> (2) <f:selectItem> (as suggested by others) should work. But if it doesn't then try below:

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

这篇关于无法在icefaces(jsf)xhtml页面上检索arraylist元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:49