[英]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");
}
我是jsf
和icefaces
,因此不確定這里的問題是什么。 有什么建議么?
更新
所以我的表中沒有任何內容,但是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.