2014-12-16 7 views
1

저는 여전히 h : selectOneMenu에서 작업하고 있으며 값 목록을 생성 할 수 있지만 변환기가 올바른 작업을 수행하지 못하고 있습니다. 나는 그 문제를 알아낼 수 없다. java.lang.ClassCastException가 :
가에 의한 java.lang.Long의이 com.ray.adtf.jpa.Gridmaster원인 : java.lang.ClassCastException : java.lang.Long을 캐스팅 할 수 없습니다.

캐스트 할 수없는 어떤 도움 주셔서 감사합니다!

XHTML :

<h:selectOneMenu id="mypick" 
      converter="#{categoryConverterBean}" 
      value="#{gridMaster_backing.pickedGrid}" 
      title="Test" > 
      <f:selectItems value="#{gridMaster_backing.gridList}" var="prog"  itemValue="#{prog.gridid}" itemLabel="#{prog.gridid} - #{prog.program} - #{prog.project} - #{prog.ci}" /> 
     </h:selectOneMenu> 

JPA :

import java.io.Serializable; 
import javax.persistence.*; 
import java.math.BigDecimal; 
import java.util.List; 
/** 
* The persistent class for the GRIDMASTER database table. 
* 
*/ 
@Entity 
@NamedQuery(name="Gridmaster.findAll", query="SELECT g FROM Gridmaster g") 
public class Gridmaster implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    private long gridid; 

    private String ci; 


    @Column(name="\"PROGRAM\"") 
    private String program; 

    private String project; 

    public Gridmaster() { 
    } 

    public Long getGridid() { 
     return this.gridid; 
    } 

    public void setGridid(Long gridid) { 
     this.gridid = gridid; 
    } 

    public String getCi() { 
     return this.ci; 
    } 

    public void setCi(String ci) { 
     this.ci = ci; 
    } 


    public String getProgram() { 
     return this.program; 
    } 

    public void setProgram(String program) { 
     this.program = program; 
    } 

    public String getProject() { 
     return this.project; 
    } 

    public void setProject(String project) { 
     this.project = project; 
    } 

변환기 :

import javax.faces.bean.ManagedBean; 
import com.ray.adtf.jpa.Gridmaster; 
import javax.faces.component.UIComponent; 
import javax.faces.context.FacesContext; 
import javax.faces.convert.Converter; 
import javax.faces.convert.FacesConverter; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 
//You must annotate the converter as a managed bean, if you want to inject 
//anything into it, like your persistence unit for example. 
@ManagedBean(name = "categoryConverterBean") 
@FacesConverter(value = "categoryConverter") 
public class CategoryConverter implements Converter { 

@PersistenceContext(unitName = "Test-Persistence") 
// I include this because you will need to 
// lookup your entities based on submitted values 
private transient EntityManager em; 

@Override 


    public Object getAsObject(FacesContext ctx, UIComponent component, 
      String value) { 
     // This will return the actual object representation 
     // of your Category using the value (in your case 52) 
     // returned from the client side 
     return em.find(Gridmaster.class, new Long(value)); 
    } 

    @Override 
    public String getAsString(FacesContext fc, UIComponent uic, Object o) { 
     //This will return view-friendly output for the dropdown menu 
     return ((Gridmaster) o).getGridid().toString(); 
    } 
    } 

백업 콩 :

public Long pickedGrid; 

    public Long getPickedGrid() { 
     return pickedGrid; 
    } 

    public void setPickedgrid(Long pickedGrid) { 
     this.pickedGrid = pickedGrid; 
    } 


Stack Trace 
javax.servlet.ServletException: java.lang.Long cannot be cast to com.ray.adtf.jpa.Gridmaster 
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659) 
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) 
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) 
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) 
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) 
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) 
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) 
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) 
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) 
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) 
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) 
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) 
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) 
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) 
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) 
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) 
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) 
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) 
io.undertow.server.Connectors.executeRootHandler(Connectors.java:177) 
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727) 
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
java.lang.Thread.run(Unknown Source) 
+0

전체 스택 트레이스를 제공 할 수 있습니까? – Everv0id

+0

은 전체 스택 추적을 추가했지만 int를 시도하고 같은 문제가 발생했기 때문에 Long이 문제라는 것을 확신하지 못했습니다 ... 일식에서이 오류도 표시됩니다. com.ray.adtf.web.CategoryConverter.getAsString (CategoryConverter. java : 33) [classes :] – GMan1973

답변

0

유형 int에 당신의 selectOneMenuGridmaster 속성을 결합하는 백업 빈의 개체로 동등하게 저장할 때 : 당신의 변환이 일을

public Gridmaster pickedGrid; 

public Gridmaster getPickedGrid() { 
    return pickedGrid; 
} 

public void setPickedgrid(Gridmaster pickedGrid) { 
    this.pickedGrid = pickedGrid; 
} 

때문에 ID를 오브젝트로 변환하여 ID를 저장할 필요가 없습니다.

+0

웹 사이트에 스레드가 있습니다 : 변환 오류 설정 값 '52'for 'null Converter'매우 도움이되었고 내 문제가 해결되었습니다. 고맙습니다! – GMan1973

0

당신은 시도 할 수 long 유형

@Id 
private int gridid; 

public int getGridid() { 
    return this.gridid; 
} 

public void setGridid(int gridid) { 
    this.gridid = gridid; 
} 
관련 문제