java.lang.ClassCastException:无法将java.util.ArrayList强制转换为javax.faces.model.SelectItem [英] java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.faces.model.SelectItem

查看:133
本文介绍了java.lang.ClassCastException:无法将java.util.ArrayList强制转换为javax.faces.model.SelectItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使下拉菜单动态化,以下是我得到的代码和异常.请帮帮我

I am trying to make my dropdown dynamic and below is the code and exception i am getting. Please help me out

bean:

    private String dropDownValue;
    List<SelectItem> testDropDown = new ArrayList<SelectItem>();
    List<SelectItem> testDropDownTwo = new ArrayList<SelectItem>();

    public String getDropDownValue() {
        return dropDownValue;
    }


    public void setDropDownValue(String dropDownValue) {
        this.dropDownValue = dropDownValue;
    }


    public List<SelectItem> getTestDropDown() {
        testDropDown.add(new SelectItem("One"));
        testDropDown.add(new SelectItem("Two"));
        testDropDown.add(new SelectItem("Three"));
        testDropDown.add(new SelectItem("Four"));
        return testDropDown;
    }

XHtml代码:

<h:selectOneMenu value="#{loginBean.dropDownValue}">
    <f:selectItem value="#{loginBean.testDropDown}" />
</h:selectOneMenu>

例外:

例外

    javax.servlet.ServletException: java.util.ArrayList cannot be cast to javax.faces.model.SelectItem
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)

    root cause 
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.faces.model.SelectItem
    com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:185)               com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:131)
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:758)
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:840)
    com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:294)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:879)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)

某些类广播异常正在发生.但是我正在将SelectItem的ArrayList发送到列表中

Some classcast exception is happening. but i am sending the ArrayList of SelectItem to the list

如果我将selectItem标记中的Value属性更改为itemValue,则不会引发异常,但不会获得任何值,我会获得对象名称,如在单个列表中的下拉列表中一样

if i change the Value attribute in selectItem tag to itemValue it is not trowing a exception but i am not getting any value i am getting the object names as in the drop down in a single list

推荐答案

您必须使用f:selectItems:

<h:selectOneMenu value="#{loginBean.dropDownValue}">
    <f:selectItems value="#{loginBean.testDropDown}"/>
</h:selectOneMenu>

f:selectItem用于在菜单中添加单个SelectItem,要添加SelectItems的整个List,则必须使用此标记.

f:selectItem is used to add a single SelectItem to the menu, to add a whole List of SelectItems you have to use this tag.

这篇关于java.lang.ClassCastException:无法将java.util.ArrayList强制转换为javax.faces.model.SelectItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆