无法显示 Jsp 中的数据
Cannot display data in Jsp
我有一个小问题,我只是想在我的 table 中显示数据,它在 3 天前工作,但我不知道我做了什么。我正在输入代码片段在这里。预先感谢您的帮助。
我的 Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
listBook(request, response);
}private void listBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Book> books = book.getAllBooks();
request.setAttribute("books", books);
this.getServletContext().getRequestDispatcher(VUE_LISTBOOK).forward(request, response);
}
BookImpl :
@Override
public List<Book> getAllbooks() {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Book> books = new ArrayList<Book>();
try {
c = MysqlDaoFactory.getInstance().getConnection();
ps = c.prepareStatement(SQL_SELECT);
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setIdBook(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setIsbn(rs.getString("isbn"));
book.setEdition(rs.getString("edition"));
book.setPublicationDate(rs.getDate("publicationDate"));
book.setResume(rs.getString("resume"));
book.setNbPage(rs.getInt("nbPage"));
book.setBarcode(rs.getString("barcode"));
book.setImage(rs.getString("image"));
book.setCategory(category.findById(rs.getInt("id")));
books.add(book);
}
}
类别实现:
private Category findById(String sql, int id) {
Connection connection = null;
Category category=null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = MysqlDaoFactory.getInstance().getConnection();
ps = connection.prepareStatement("SELECT * from category where id=?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
category = new Category();
int id2 = Integer.parseInt(rs.getString("id"));
String category1 = rs.getString("category");
category.setId(id2);
category.setCategory(category1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
MysqlConnectionFactory.closeConnection(connection);
MysqlConnectionFactory.closeResultSet(rs);
}
return category;
}
}
JSP :
<c:forEach items="${books}" var="books">
<tr>
<th><c:out value="${books.idBook}"/></th>
<th><c:out value="${books.title}"/></th>
<th><c:out value="${books.isbn}"/></th>
<th><c:out value="${books.edition}"/></th>
<th><c:out value="${books.publicationDate}"/></th>
<th><c:out value="${books.resume}"/></th>
<th><c:out value="${books.barcode}"/></th>
<th><c:out value="${books.image}"/></th>
<th><c:out value="${books.category.category}"/></th>
</tr>
</c:forEach>
输出:
<!DOCTYPE html>
<!-- meta -->
<meta charset="utf-8">
<tr>
<th>test</th>
<th>test</th>
<th>test</th>
<th>test</th>
<th>1111-05-05</th>
<th>test</th>
<th>test</th>
<th>test</th>
<th></th> ---> Empty
我认为问题出在“FindById”方法上
抱歉,如果不清楚
问题已经解决,我拿错ID了,可以关闭这个主题吗?
我有一个小问题,我只是想在我的 table 中显示数据,它在 3 天前工作,但我不知道我做了什么。我正在输入代码片段在这里。预先感谢您的帮助。
我的 Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
listBook(request, response);
}private void listBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Book> books = book.getAllBooks();
request.setAttribute("books", books);
this.getServletContext().getRequestDispatcher(VUE_LISTBOOK).forward(request, response);
}
BookImpl :
@Override
public List<Book> getAllbooks() {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Book> books = new ArrayList<Book>();
try {
c = MysqlDaoFactory.getInstance().getConnection();
ps = c.prepareStatement(SQL_SELECT);
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setIdBook(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setIsbn(rs.getString("isbn"));
book.setEdition(rs.getString("edition"));
book.setPublicationDate(rs.getDate("publicationDate"));
book.setResume(rs.getString("resume"));
book.setNbPage(rs.getInt("nbPage"));
book.setBarcode(rs.getString("barcode"));
book.setImage(rs.getString("image"));
book.setCategory(category.findById(rs.getInt("id")));
books.add(book);
}
}
类别实现:
private Category findById(String sql, int id) {
Connection connection = null;
Category category=null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = MysqlDaoFactory.getInstance().getConnection();
ps = connection.prepareStatement("SELECT * from category where id=?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
category = new Category();
int id2 = Integer.parseInt(rs.getString("id"));
String category1 = rs.getString("category");
category.setId(id2);
category.setCategory(category1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
MysqlConnectionFactory.closeConnection(connection);
MysqlConnectionFactory.closeResultSet(rs);
}
return category;
}
}
JSP :
<c:forEach items="${books}" var="books">
<tr>
<th><c:out value="${books.idBook}"/></th>
<th><c:out value="${books.title}"/></th>
<th><c:out value="${books.isbn}"/></th>
<th><c:out value="${books.edition}"/></th>
<th><c:out value="${books.publicationDate}"/></th>
<th><c:out value="${books.resume}"/></th>
<th><c:out value="${books.barcode}"/></th>
<th><c:out value="${books.image}"/></th>
<th><c:out value="${books.category.category}"/></th>
</tr>
</c:forEach>
输出:
<!DOCTYPE html>
<!-- meta -->
<meta charset="utf-8">
<tr>
<th>test</th>
<th>test</th>
<th>test</th>
<th>test</th>
<th>1111-05-05</th>
<th>test</th>
<th>test</th>
<th>test</th>
<th></th> ---> Empty
我认为问题出在“FindById”方法上 抱歉,如果不清楚
问题已经解决,我拿错ID了,可以关闭这个主题吗?