1

我已经采取了Collection<Edition> selectedEditions;。当我这样迭代时:

    Collection<Edition> edlist=(java.util.Collection)selectedEditions;         
for(Edition ed:edlist){ // error at this line
            EditionID=ed.getEditionID();
            NewspaperID=ed.getNewspaper().getNewspaperID();
            StateID=ed.getCity().getState().getStateID();
            System.out.print("nid..........."+NewspaperID);
            System.out.print("sid..........."+StateID);
        } 

然后它给出如下错误:java.lang.ClassCastException: java.lang.String cannot be cast to entity.Edition
my getter setter:

public Collection<Edition> getSelectedEditions() {
                return selectedEditions;
}
public void setSelectedEditions(Collection<Edition> selectedEditions) {
        this.selectedEditions = selectedEditions;
    } 

                              </h:selectManyCheckbox>
                            <h:dataTable id="dt1" value="#{adcreateBean.selectedEditions}"  var="it" styleClass="nostyle" width="100%">
                                        <f:facet name="header">
                                            <h:outputText value="You have selected :" />
                                        </f:facet>
                                        <h:column>
                                            <h:outputText value="#{it}" />
                                     </h:column>
                                   </h:dataTable>

那么,我怎样才能转换为 entity.Edition?正如在回答这个问题时所说的([我如何在 jsf 中获得多选复选框值?),我该如何转换?

如何在 jsf 中获取多选复选框值?

4

1 回答 1

2

正如您在上一个问题中评论/回答的那样,您只需要创建一个在和Converter之间转换的自定义。您只需要创建一个实现的类,然后相应地实现这些方法。StringEditionjavax.faces.convert.Converter

@FacesConverter("editionConverter")
public class EditionConverter implements Converter {

    @Override
    public Object getAsString(FacesContext context, UIComponent component, Object object) {
        // Write code yourself which converts from Edition object to its unique 
        // String representation. This string will then be used in HTML and HTTP.
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        // Write code yourself which converts from unique String representation
        // back to Edition object. This object will then be used in JSF model.
    }

}

通常,技术 ID 用作唯一String表示。这是一个基本的启动示例:

@FacesConverter("editionConverter")
public class EditionConverter implements Converter {

    @Override
    public Object getAsString(FacesContext context, UIComponent component, Object object) {
        Long id = (object instanceof Edition) ? ((Edition) object).getId() : null;
        return (id != null) ? String.valueOf(id) : null;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        Long id = (submittedValue != null) ? Long.valueOf(submittedValue) : null;
        return (id != null) ? someEditionService.find(id) : null;
    }

}

最后在<h:selectManyCheckbox>.

<h:selectManyCheckbox ... converter="editionConverter">

请注意,这与铸造绝对不同。Edition根本不是的超类或子类String。要了解究竟什么是强制转换,请阅读Oracle 自己的关于该主题的基本 Java 教程

于 2012-06-17T13:41:59.340 回答