简体   繁体   中英

Servlets , JSP, JPA - Property Not Found Exception

I'm writing my first bigger app and I have one issue, my code below:

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>

In index page I have listed categories, and when i want go to choosen category and display books for this category i got this exception:

org.apache.jasper.el.JspPropertyNotFoundException: /booksCategory.jsp(40,4) '${booksDao.booksByCategory}' Property 'booksByCategory' not found on type DAO.BooksDAO

Can someone tell me what I did wrong?

You're calling a method thinking you're calling for an actual object. I'd create a List<Books> object in DAOBooks and send it with the request to the JSP.


Solution

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;
}

And in your JSP

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

Make a direct reference to the List<Books> object because it is the one you're sending via the request.


EDIT

Try to have a distinct setter and getter method. It'll help you have a more readable code and will solve those type of problems instantly.

I made an example in my post but it is not necessarily a correct one, you have to find the ways to implement them following your application logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM