我仍在使用我的h:selectOneMenu,我能够生成一个很好的值列表,但转换器没有做正确的事情,我无法弄清楚问题。
引起:java.lang.ClassCastException:java.lang.Long无法强制转换为com.ray.adtf.jpa.Gridmaster
感谢您的帮助!
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();
}
}
支持bean:
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 :(得分:0)
您可以尝试:long
输入int
类型
@Id
private int gridid;
public int getGridid() {
return this.gridid;
}
public void setGridid(int gridid) {
this.gridid = gridid;
}
答案 1 :(得分:0)
将Gridmaster
属性绑定到selectOneMenu
时,将其作为对象存储在辅助bean中:
public Gridmaster pickedGrid;
public Gridmaster getPickedGrid() {
return pickedGrid;
}
public void setPickedgrid(Gridmaster pickedGrid) {
this.pickedGrid = pickedGrid;
}
因为您的转换器负责将ID转换为对象,因此您不必存储ID。