Unable to retrieve arraylist elements on icefaces(jsf) xhtml page

ぐ巨炮叔叔 提交于 2020-01-15 11:24:00

问题


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>

My managed bean looks like:

public ArrayList<String> getProperties()
{
    return 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");
}

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

Update

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

Update 2

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]

Update 3: Current 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>

Update 4: Namespace declaration

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

Update5

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"));
}

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>

回答1:


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.

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>



回答2:


Why do you use namespace p for <p:selectItem value="#{beanInfo.properties}"/>? p is usually primefaces components, I'm not sure it is good practice to mix component libraries. Try jsf's standard <f:selectItems value="#{beanInfo.properties"/>
Note, that you should use selectItems not selectItem when you use list as value.

Updated
Try

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



回答3:


You should use selectitem in stead of string in your getter

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

and fill your properties with selectitems

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


来源:https://stackoverflow.com/questions/9723118/unable-to-retrieve-arraylist-elements-on-icefacesjsf-xhtml-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!