gpt4 book ai didi

java.lang.ClassCastException : [Ljava. lang.Object;以及如何转换类

转载 作者:行者123 更新时间:2023-11-29 09:19:35 25 4
gpt4 key购买 nike

我的 DAO 是

public class bookRequestStatusDAO {

public static List<Book> getbookRequest()
{

Session session=null;
try
{
List booklist=new ArrayList();

session = HibernateUtil.getSession();
System.out.println(" Session :: "+session);
Query query=session.createSQLQuery("{call SP_bookRequestTable()}");
booklist=query.list();
System.out.println("in dao getrequest----"+booklist.size());
if(booklist!=null)
return booklist;
else
return new ArrayList();
}
catch(Exception e)
{
System.out.println("Exception in BookREqueststatus::"+e);
return null;
}
finally
{
session.close();

}

}

我的 Struts Action 类是

public ActionForward execute(ActionMapping mapping, ActionForm  form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println(" exce -----");
BookrequeststatusForm bookrequeststatusForm=(BookrequeststatusForm)form;
List<Book> convertlist=new ArrayList<Book>();
convertlist=bookRequestStatusDAO.getbookRequest();


System.out.println("list in Action::" +convertlist.size() );
request.setAttribute("bookDeatils",convertlist );
System.out.print("request in Action::"+request.getAttribute("bookDeatils"));
return mapping.findForward(Constants.ACTION_FORWARD_SUCCESS);

}
}

在我的jsp中

<link rel="stylesheet" href="/REC_BookBank/css/bookRequestStatus.css">
<%
List<Book>list=new ArrayList<Book>();
if(request.getAttribute("bookDeatils")!=null)
{

list = (ArrayList)request.getAttribute("bookDeatils");
System.out.println("inside loop List --- "+list.size());
for(Book book : list)
{
out.println("<tr><td>"+book.getId()+"</td></tr>");
}
}
System.out.println(" List --- "+list.size());
%>

我的书.java

import java.io.Serializable;

/**
*
* @author Administrator
*/
public class Book implements Serializable {
private String id;
private String subject;
private String title;
private String author;
private String publication;
private String edition;
private String status;
private String mode;

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getEdition() {
return edition;
}

public void setEdition(String edition) {
this.edition = edition;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getMode() {
return mode;
}

public void setMode(String mode) {
this.mode = mode;
}

public String getPublication() {
return publication;
}

public void setPublication(String publication) {
this.publication = publication;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

}

在 tomcat 领事中

 exce -----
Session :: SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])
Hibernate: {call SP_bookRequestTable()}
in dao getrequest----1
list in Action::1
request in Action::[[Ljava.lang.Object;@14ef239]inside loop List --- 1

异常HTTP 状态 500 -


类型异常报告

留言

描述服务器遇到内部错误 (),导致它无法完成此请求。

异常在

org.apache.jasper.JasperException: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.rec.bookbank.persitencejavafile.Book
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)
org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)
org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


root cause

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.rec.bookbank.persitencejavafile.Book
org.apache.jsp.jsp.bookRequestStatus_jsp._jspService(bookRequestStatus_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)
org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)
org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.

我需要在此处打印值,但它显示异常 ClassCastException

最佳答案

你得到异常的原因是因为对存储过程的调用返回了一个不能直接转换为类的值数组。您可以执行以下两件事之一来获取书籍列表而不是对象列表 []。

  • 您可以在 .hbm.xml 文件中映射过程并调用它。 ( example )
  • 您可以使用 RowMapper 类实例来映射行。 (这在 spring 中可用)。

我个人更喜欢映射过程,它更优雅。

迭代示例:

// can be used in DAO to convert the object array into List of Objects
public List<Book> getList(List<Object[]> bookResult){
List<Book> books = new ArrayList<Book>();
for(Object[] array:bookResult){
Book book= new Book();
book.setTitle(arr[0]); // you need to set the value of each pojo field here.
books.add(book);
}
return books;
}

关于java.lang.ClassCastException : [Ljava. lang.Object;以及如何转换类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7280324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com