我一直试图获取并打印数据表。我认为我在servlet中正确设置了请求但是当我尝试获取test.jsp中的book列表时,它说服务器遇到意外情况,导致它无法完成请求。 他们应该去哪里?我觉得我错过了什么或者他们完全错了,但我无法弄清楚是什么。 谢谢你的帮助。
servletTest中的
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String userPath="/test";
String url = "/WEB-INF/jsp"+userPath+".jsp";
String categoryName = request.getParameter("category");
request.setAttribute("selectedCategoryName", categoryName);
CategoryDao categoryDao =
ApplicationContext.INSTANCE.getCategoryDao();
BookDao bookDao = ApplicationContext.INSTANCE.getBookDao();
request.setAttribute("bookList",bookDao);
request.getRequestDispatcher(url).forward(request,response);
}
在test.jsp
中<body>
<ul>
<li><a href="test?category=architecture">architecture</a></li>
<li><a href="test?category=fine art">fine art</a></li>
<li><a href="test?category=fashion">fashion</a></li>
<li><a href="test?category=photography">photography</a></li>
<li><a href="test?category=graphic">graphic</a></li>
</ul>
<c:set var="categoryName">${selectedCategoryName}</c:set>
<h1><span style="size: 14px">${categoryName}</span> </h1>
<table border="1">
<tr>
<th>book_id</th>
<th>title</th>
<th>author</th>
<th>price</th>
<th>is_public</th>
<th>category_id</th>
</tr>
<c:forEach var="book" items="${bookList}">
<tr>
<td>${book.bookId}</td>
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>${book.is_public}</td>
<td>${book.category_id}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
预订课程
public class Book {
private long bookId;
private String title;
private String author;
private int price;
private boolean isPublic;
private Long categoryId;
public Book(long bookId, String title, String author, int price,
boolean isPublic, long categoryId) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.price = price;
this.isPublic = isPublic;
this.categoryId = categoryId;
}
public long getBookId() {
return bookId;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getPrice() {
return price;
}
public boolean getIsPublic() {
return isPublic;
}
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", title='" + title + '\'' +
", author='" + author + '\'' +
", price=" + price +
", isPublic=" + isPublic +
", categoryId=" + categoryId +
'}';
}
}
答案 0 :(得分:0)
错误是因为您没有调用正确的名称。在您的图书课程中,您有以下变量:
private boolean isPublic;
private Long categoryId;
但是在你的jsp中,你可以调用这些变量:
<td>${book.is_public}</td>
<td>${book.category_id}</td>
哪个错了。你需要打电话:
<td>${book.isPublic}</td>
<td>${book.categoryId}</td>
然后它会起作用。确保名称始终相同。