[英]Unable to retrieve arraylist elements on icefaces(jsf) xhtml page
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: 我在我的页面上有
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>
My managed bean looks like: 我的托管bean看起来像:
public ArrayList<String> getProperties()
{
return properties;
}
and in constructor am populating properties
as shown: 并在构造函数中填充
properties
,如下所示:
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. 我是
jsf
和icefaces
,因此不确定这里的问题是什么。 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. 所以我的表中没有任何内容,但是
java.util.ArrayList cannot be cast to javaax.faces.model.SelectItem
异常。
Update 2 更新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. 这是我在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]
Update 3: Current xhtml 更新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>
Update 4: Namespace declaration 更新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 Update5
I was able to fix the exception by using array list of SelectItem types rather then String so in my bean, I have: 我能够通过使用SelectItem类型的数组列表而不是String来修复异常,所以在我的bean中,我有:
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: 在我的
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>
It is causing ClasscastException because in constructor of bean you are making a collection of type String ie Arraylist<String>
while JSF uses collection of type SelectItem ie Arraylist<SelectItems>
. 它导致ClasscastException,因为在bean的构造函数中,您正在创建String类型的集合,即
Arraylist<String>
而JSF使用SelectItem类型的集合,即Arraylist<SelectItems>
。 When with current setting page renders it thows ClasscastException, which s obvious. 当使用当前设置页面呈现时,它会抛出ClasscastException,这很明显。
Posible Fix: (1) change the type of collection in constructor. Posible Fix: (1)更改构造函数中的集合类型。 Make it
Arraylist<SelectItem>
(2) <f:selectItem>
(as suggested by others) should work. 使其成为
Arraylist<SelectItem>
(2) <f:selectItem>
(由其他人建议)应该有效。 But if it doesn't then try below: 但如果没有,那么尝试下面:
<ice:selectOneMenu value="myProperties">
<ice:selectItems value="#{beanInfo.properties}" />
</ice:selectOneMenu>
Why do you use namespace p
for <p:selectItem value="#{beanInfo.properties}"/>
? 为什么使用命名空间
p
来表示<p:selectItem value="#{beanInfo.properties}"/>
? p
is usually primefaces components, I'm not sure it is good practice to mix component libraries. p
通常是primefaces组件,我不确定混合组件库是一个好习惯。 Try jsf's standard <f:selectItems value="#{beanInfo.properties"/>
尝试jsf的标准
<f:selectItems value="#{beanInfo.properties"/>
Note, that you should use selectItems not selectItem when you use list as value. 请注意,当您使用list作为值时,应该使用selectItems而不是selectItem。
Updated 更新
Try 尝试
<f:selectItems value="#{beanInfo.properties}"
var="property"
itemValue="#{property}"
itemLabel="#{property}"/>
You should use selectitem in stead of string in your getter 您应该在getter中使用selectitem而不是字符串
public ArrayList<SelectItem> getProperties() {
return properties;
}
and fill your properties with selectitems 并使用选择项填充您的属性
properties.add(new SelectItem(<the value>, <text to display>));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.