簡體   English   中英

無法檢索icefaces(jsf)xhtml頁面上的arraylist元素

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

我在我的頁面上有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的方法之后獲得的例外,並將我的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">

Update5

我能夠通過使用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>

它導致ClasscastException,因為在bean的構造函數中,您正在創建String類型的集合,即Arraylist<String>而JSF使用SelectItem類型的集合,即Arraylist<SelectItems> 當使用當前設置頁面呈現時,它會拋出ClasscastException,這很明顯。

Posible Fix: (1)更改構造函數中的集合類型。 使其成為Arraylist<SelectItem> (2) <f:selectItem> (由其他人建議)應該有效。 但如果沒有,那么嘗試下面:

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

為什么使用命名空間p來表示<p:selectItem value="#{beanInfo.properties}"/> p通常是primefaces組件,我不確定混合組件庫是一個好習慣。 嘗試jsf的標准<f:selectItems value="#{beanInfo.properties"/>
請注意,當您使用list作為值時,應該使用selectItems而不是selectItem。

更新
嘗試

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

您應該在getter中使用selectitem而不是字符串

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

並使用選擇項填充您的屬性

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM