2012-06-17 4 views
1

나는 이런 식으로 반복 Collection<Edition> selectedEditions; .When을 촬영 한 :jsf에서 컬렉션으로 엔티티 개체를 캐스팅하는 방법?

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의는 entity.Edition
내 게터 캐스트 할 수없는 세터 :

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> 

그럼 어떻게 엔티티를 추가 할 수 있습니까? 이 질문에 대한 대답 ([How can I get multiselected checkbox value in jsf?)]에서 어떻게 변환 할 수 있습니까?

How can I get multiselected checkbox value in jsf?

답변

2

으로 방금 StringEdition 사이에 변환하는 사용자 정의 Converter를 만들어야합니다,/주석 your previous question에 대답했다. javax.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"> 

이 절대적으로 하지 캐스팅과 동일 함을 유의하시기 바랍니다. EditionString의 수퍼 클래스 또는 하위 클래스가 아닙니다. 정확한 전송이 무엇인지 이해하려면 Oracle's own basic Java tutorial on the subject을 읽어보십시오.

관련 문제