Servlets,JSP,JPA - 未找到属性异常

时间:2015-12-05 20:11:54

标签: java jsp jpa servlets

我正在编写我的第一个更大的应用程序,我有一个问题,我的代码如下:

InitDB.java

public void requestInitialized(ServletRequestEvent arg0)  { 
    EntityManager em = DBConfig.createEntityManager();
    BooksDAO booksDAO = new BooksDAO(em);
    CategoryDAO categoriesDAO = new CategoryDAO(em);
    ServletRequest req = arg0.getServletRequest();
    req.setAttribute("booksDao", booksDAO);
    req.setAttribute("categoriesDao", categoriesDAO);
}

BooksDAO.java

EntityManager em = DBConfig.createEntityManager();

public BooksDAO(EntityManager em) {
    this.em = em;
}
public List<Books> getBooksByCategory(String category) {
    Query q = this.em.createQuery("SELECT b FROM Books b WHERE b.category = :category ", Books.class).setParameter("category", category);
    List<Books> booksByCategory = q.getResultList();
    return booksByCategory;
}

booksCategoryServlet.java

@WebServlet("/booksCategory")
public class booksCategoryServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String category = request.getParameter("category");
        if (category != null) {
            BooksDAO dao = (BooksDAO) request.getAttribute("booksDao");
            List<Books> booksByCategory = dao.getBooksByCategory(category);
            request.setAttribute("booksByCategory", booksByCategory);
            request.getRequestDispatcher("/booksCategory.jsp").forward(request, response);
        } else
            response.sendRedirect(request.getContextPath() + "/");
    }

bookCategory.jsp

<c:forEach var="book" items="${booksDao.booksByCategory}">
    <tr>
        <td>${book.title}</td>
        <td>${book.author}</td>
        <td>${book.description}</td>
        <td>${book.category}</td>
        <td>${book.year}</td>
        <td><a href="bookDetails?id=${book.id}">show details</a></td>
    </tr>
</c:forEach>

的index.jsp

<c:forEach var="category" items="${categoriesDao.categories}">
    <a href="booksCategory?category=${category}"><li>${category}</li></a>
</c:forEach>

在索引页面中,我列出了类别,当我想要选择类别并显示此类别的书籍时,我遇到了这个例外:

  

org.apache.jasper.el.JspPropertyNotFoundException:/booksCategory.jsp(40,4)'$ {booksDao.booksByCategory}'类型DAO.BooksDAO'找不到属性'booksByCategory'

有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:2)

您正在调用一种方法,认为您正在调用实际对象。 我在List<Books>中创建了一个DAOBooks对象,并将请求发送给JSP。

解决方案

<强> DAOBooks

EntityManager em = DBConfig.createEntityManager();
List<Books> booksByCategory = new ArrayList<>(); // Or whatever list type you need.

public BooksDAO(EntityManager em) {
    this.em = em;
}
public void setBooksByCategory(String category) {
    Query q = this.em.createQuery("SELECT b FROM Books b WHERE b.category = :category ", Books.class).setParameter("category", category);
    booksByCategory = q.getResultList();
}

public List<Books> getBooksByCategory(){
    return booksByCategory;
}

JSP

<c:forEach var="book" items="${booksByCategory}">

直接引用List<Books>对象,因为它是您通过请求发送的对象。

修改

尝试使用不同的settergetter方法。它将帮助您获得更易读的代码,并立即解决这些类型的问题。

我在我的帖子中做了一个例子,但它不一定是正确的,你必须找到按照你的应用程序逻辑实现它们的方法。