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

时间:2011-09-02 07:23:34

标签: java jsp struts

我的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 Class是

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());
%>

my Book.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 -


输入例外报告

消息

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

中的

异常

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

1 个答案:

答案 0 :(得分:0)

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

  • 您可以在.hbm.xml文件中映射该过程&amp;叫它。 (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;
    }