当尝试从数据库中获取机械行列表并传递到jsp页面时,出现以下错误“ Ljava.lang.Object;无法转换为com.entity.MechanicEntity”。
机械实体
@Entity
@Table(name="Mechanic")
public class MechanicEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
int mechanicId;
String mechanicType;
int numberOfVehicles;
public int getMechanicId() {
return mechanicId;
}
public void setMechanicId(int mechanicId) {
this.mechanicId = mechanicId;
}
public String getMechanicType() {
return mechanicType;
}
public void setMechanicType(String mechanicType) {
this.mechanicType = mechanicType;
}
public int getNumberOfVehicles() {
return numberOfVehicles;
}
public void setNumberOfVehicles(int numberOfVehicles) {
this.numberOfVehicles = numberOfVehicles;
}
}
RegisterController.java
...
RegisterService service;
@ModelAttribute("MechList")
public Map<Integer,Integer> populateMechId()throws Exception {
List<Mechanic> allMech = service.getAllMechanic();
Map<Integer,Integer> MechanicMap=new HashMap<Integer,Integer>();
for(Mechanic mechanic:allMech){
MechanicMap.put(mechanic.getMechanicId(),mechanic.getMechanicId());
}
return MechanicMap;
} ...
RegisterService.java
...
RegisterDAO dao;
public List<Mechanic> getAllMechanic() throws Exception
{
return dao.getAllMechanic();
}
...
RegisterDAO.java
public List<Mechanic> getAllMechanic() throws Exception
{
List<Mechanic> mlist=new ArrayList<Mechanic>();
Query q=em.createQuery("select m.mechanicId,m.mechanicType from MechanicEntity m");
List<MechanicEntity> l1=q.getResultList();
for(MechanicEntity m:l1)
{
Mechanic mechanic=new Mechanic();
mechanic.setMechanicId(m.getMechanicId());
mlist.add(mechanic);
}
return mlist;
}
registerVehicle.jsp
<form:form name="registerForm1" action="registerVehicle.htm" modelAttribute="serviceregister" method="GET" >
<form:select path="mechanicId">
<form:options items="${MechList}" />
</form:select>
</form:form>
错误:
Type Exception Report
Message org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:594)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:495)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:750)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:720)
org.apache.jsp.index_jsp._jspService(index_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity
com.dao.RegisterDAO.getAllMechanic(RegisterDAO.java:19)
com.service.RegisterService.getAllMechanic(RegisterService.java:27)
com.controller.RegisterController.populateMechId(RegisterController.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:750)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:720)
org.apache.jsp.index_jsp._jspService(index_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
请帮助我进行纠正,因此在加载视图时会显示下拉列表。
答案 0 :(得分:1)
由于以下原因,您的代码甚至无法编译
:Query q=em.createQuery("select m.mechanicId,m.mechanicType from MechanicEntity m");
List<MechanicEntity> l1=q.getResultList();
问题是getResultList()
返回List<Object>
而不是List< MechanicEntity>
。
要使其正常工作:
List<MechanicEntity> l1 = createQuery("select m from MechanicEntity m", MechanicEntity.class).getResultList();