java.lang.ClassCastException:java.util.Collections $ UnmodifiableMap无法强制转换为Param

时间:2013-11-05 15:58:12

标签: jsf converter selectonemenu

我正在尝试将转换器写入我的selectonemenu,我不想选择对象。 我的对象(“Param”)类看起来像这样:

public class Param {
private Integer id;
private String name;

public Param(Integer id, String name){
    this.id = id;
    this.name = name;
}

public Integer getId(){
    return this.id;
}
public String getName(){
    return this.name;
}

@Override
public boolean equals(Object object) {
    return (object instanceof Param) && (id != null) 
         ? id.equals(((Param) object).getId()) 
         : (object == this);
}

@Override
public int hashCode() {
    return (id != null)
        ? (this.getClass().hashCode() + id.hashCode())
        : super.hashCode();
}

}

豆:

@ManagedBean
@SessionScoped
public class MainBean {
private List<Param> params;
private Param selectedParam;


public List<Param> getParams(){
    return this.params;
}
public Param getSelectedParam(){
    return this.selectedParam;
}
public void setSelectedParam(Param param){
    this.selectedParam = param;
}


public MainBean(){
    //filling params - works good
}

}

转换器(我知道应该是空检查和东西):

@FacesConverter(value = "paramConverter", forClass = Param.class)
public class ParamConverter implements Converter {
private List<Param> getParams(){
    ExternalContext tmpEC;
    Map sMap;
    tmpEC = FacesContext.getCurrentInstance().getExternalContext();
    sMap = tmpEC.getSessionMap();
    MainBean mainBean = (MainBean) sMap.get("mainBean");
    return mainBean.getParams();
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
    Integer id = Integer.valueOf(value);
    List<Param> params = this.getParams();
    Param param = null;
    for(int i=0; i < params.size(); i++){
        if(id == params.get(i).getId()){
            param = params.get(i);
            break;
        }
    }
    return param;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
    Param param = (Param)value;
    return String.valueOf(param.getId());
}

}

selectOneMenu用于:

<h:selectOneMenu value="#{mainBean.selectedParam}">
<f:converter converterId="paramConverter" />
<f:selectItems value="#{mainBean.params}"
var="param" 
itemValue="#{param}" 
itemLabel="#{param.name}" 
/>
</h:selectOneMenu>

Tomcat输出:

HTTP Status 500 - java.util.Collections$UnmodifiableMap cannot be cast to Param

--------------------------------------------------------------------------------

type Exception report

message java.util.Collections$UnmodifiableMap cannot be cast to Param

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

javax.servlet.ServletException: java.util.Collections$UnmodifiableMap cannot be cast to Param
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)


root cause 

java.lang.ClassCastException: java.util.Collections$UnmodifiableMap cannot be cast to Param
    ParamConverter.getAsString(ParamConverter.java:40)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:521)
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderOption(MenuRenderer.java:534)
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:794)
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:847)
    com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:297)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:115)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)

我认为getAsString方法将UnmodifiableMap对象作为值,但它应该是Param对象,我是对的吗?

请帮帮我

聚苯乙烯。如果我犯了一些错误,我的英语很抱歉; P

2 个答案:

答案 0 :(得分:2)

问题出现在var属性中。

<f:selectItems value="#{mainBean.params}"
    var="param" 
    itemValue="#{param}" 
    itemLabel="#{param.name}" 
/>

#{param}reserved EL variable引用的ServletRequest#getParameterMap()类型Map<String, String[]>,实际上是不可修改的地图。

您不应该为自己的变量使用保留的EL变量名。将其重命名为其他内容,例如_param左右。

<f:selectItems value="#{mainBean.params}"
    var="_param" 
    itemValue="#{_param}" 
    itemLabel="#{_param.name}" 
/>

你的转换器虽然有点笨拙且不完全健壮,但完全没问题。

答案 1 :(得分:-2)

在这里检查工作代码,并尝试自己找错。它会让你记住它有用:)

                                <h:selectOneMenu rendered="#{categoryConverterBean.rendered}" class="subcategory" id="subcategory" converter="#{subcategoryConverterBean}" value="#{auctionBean.subCategory}">  

                                    <f:selectItems 
                                        value="#{categoryListBean.subcategory}" 
                                        var="subcategory"
                                        itemLabel="#{subcategory.subName}" 
                                        itemValue="#{subcategory}" 
                                        />
                                </h:selectOneMenu>

auctionBean(没什么特别的):

public Subcategory getSubCategory() {
    return subCategory;
}

public void setSubCategory(Subcategory subCategory) {
    this.subCategory = subCategory;
}

subcategoryConverter:

@ManagedBean(name = "subcategoryConverterBean") 
@FacesConverter(value = "subcategoryConverter")
public class SubcategoryConverter implements Converter {

    @PersistenceContext()
    private transient EntityManager em;

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
        // TODO Auto-generated method stub
            System.out.println("wybrales: " + value);
        Query query = em.createQuery("SELECT c FROM Subcategory c WHERE c.subId=?1");
        query.setParameter("1", Integer.parseInt(value));

        return query.getSingleResult();
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
        // TODO Auto-generated method stub
        return ((String) String.valueOf(((Subcategory) value).getSubId()));
    }

}

最后是categoryListBean:

@ManagedBean
public class CategoryListBean {

    private List<CategoryBean> category;
    private List<SubcategoryBean> subcategory;

    @EJB
    CategoryEAO service;

    public List<CategoryBean> getCategory() {
        category = service.getCategories();
        return category;
    }

    public void setCategory(List<CategoryBean> category) {
        this.category = category;
    }

    public List<SubcategoryBean> getSubcategory() {
        subcategory = service.getSubcategories();
        return subcategory;
    }

    public void setSubcategory(List<SubcategoryBean> subcategory) {
        this.subcategory = subcategory;
    }
}

我会专注于你的getAsObject方法,因为看起来你没有像你认为的那样得到Param对象