You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArrayList无法转换为Book Bean:JSP结合ArrayList使用Bean的技术求助

Fixing java.util.ArrayList cannot be cast to bean.Book in JSP

Hey there! That error pops up when you're trying to treat a list of Book beans like a single Book bean in your JSP—super common mistake, let's sort it out step by step.

First, Make Sure Your Servlet is Storing the ArrayList Correctly

Let's finish up that Servlet code you started, since the key is properly passing the list to your JSP:

public class ResServlet extends HttpServlet{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String isbn = request.getParameter("isbn");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");
        String year = request.getParameter("year");
        
        // Create your ArrayList of Book objects
        ArrayList<Book> bookList = new ArrayList<>();
        
        // Add a Book (you might be fetching multiple from a DB here instead)
        Book book = new Book();
        book.setIsbn(isbn);
        book.setTitle(title);
        book.setAuthor(author);
        book.setPublisher(publisher);
        book.setYear(year);
        bookList.add(book);
        
        // Store the list in the request scope with a clear key
        request.setAttribute("bookList", bookList);
        
        // Forward to your JSP page
        RequestDispatcher dispatcher = request.getRequestDispatcher("/bookDisplay.jsp");
        dispatcher.forward(request, response);
    }
}

Now Access the List Properly in Your JSP

The mistake you're making is probably trying to cast the entire ArrayList to a single Book (like with <jsp:useBean> targeting the list). Instead, you need to loop through the list to access each Book bean inside it.

Option 1: Use JSTL (Recommended—Cleaner, No Scriptlets)

First, add the JSTL core taglib to the top of your JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Then loop through the list with <c:forEach>:

<div class="book-container">
    <c:forEach items="${bookList}" var="singleBook">
        <div class="book-card">
            <p><strong>ISBN:</strong> ${singleBook.isbn}</p>
            <p><strong>Title:</strong> ${singleBook.title}</p>
            <p><strong>Author:</strong> ${singleBook.author}</p>
            <p><strong>Publisher:</strong> ${singleBook.publisher}</p>
            <p><strong>Year:</strong> ${singleBook.year}</p>
        </div>
        <hr>
    </c:forEach>
</div>

Note: Make sure your Book class has public getter methods (like getIsbn(), getTitle())—EL expressions rely on these to pull values.

Option 2: Use Scriptlets (Not Recommended, But For Reference)

If you can't use JSTL for some reason, you can cast the request attribute back to an ArrayList and loop with Java code:

<%
    ArrayList<Book> bookList = (ArrayList<Book>) request.getAttribute("bookList");
    if (bookList != null && !bookList.isEmpty()) {
        for (Book singleBook : bookList) {
%>
            <div class="book-card">
                <p><strong>ISBN:</strong> <%= singleBook.getIsbn() %></p>
                <p><strong>Title:</strong> <%= singleBook.getTitle() %></p>
                <p><strong>Author:</strong> <%= singleBook.getAuthor() %></p>
                <p><strong>Publisher:</strong> <%= singleBook.getPublisher() %></p>
                <p><strong>Year:</strong> <%= singleBook.getYear() %></p>
            </div>
            <hr>
<%
        }
    }
%>

Common Pitfalls to Avoid

  • Don't do this: <jsp:useBean id="bookList" class="bean.Book" />—this tries to convert your ArrayList to a single Book, which is exactly what causes the error.
  • Double-check that the attribute name in your Servlet ("bookList") matches what you use in the JSP (${bookList})—typos here can cause weird issues too.
  • Ensure your Book class follows JavaBean conventions: private fields, public getters/setters, no-arg constructor (though most frameworks handle this, it's good practice).

内容的提问来源于stack exchange,提问作者Ilan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:51:18