2

オブジェクトを選択したくない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 オブジェクトである必要があります。

私を助けてください

Ps。私がいくつか間違いをした場合、私の英語について申し訳ありません;P

4

2 に答える 2