Digma Chauhan
Digma Chauhan

Reputation: 195

how to cast Collection to Entity Object in jsf?

I have taken Collection<Edition> selectedEditions; .When I iterate like this :

    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);
        } 

Then it gives error like: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;
    } 

and

                              </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>

So,How can I cast to entity.Edition? As told in answer of this question([How can I get multiselected checkbox value in jsf?), how can I convert?

How can I get multiselected checkbox value in jsf?

Upvotes: 1

Views: 1994

Answers (1)

BalusC
BalusC

Reputation: 1108632

As commented/answered in your previous question, you just need to create a custom Converter which converts between String and Edition. You just have to create a class which implements javax.faces.convert.Converter and then implement the methods accordingly.

@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.
    }

}

Usually, the technical ID is been used as unique String representation. Here's a basic kickoff example:

@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;
    }

}

Finally use it on the <h:selectManyCheckbox>.

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

Please note that this is absolutely not the same as casting. Edition isn't a superclass or subclass of String at all. To understand what exactly casting is, please read Oracle's own basic Java tutorial on the subject.

Upvotes: 2

Related Questions