我正在尝试将一些数据(Article对象)传递给JSP,但似乎什么都没有显示,我不确定自己做错了什么。当单击具有文章ID的超链接时,我希望能够显示文章的标题和正文。我想我一定在doGet方法上做错了。现在,当我单击超链接以显示文章时,我得到一个空白页面。
public static Article articleById(int id, Connection conn) throws SQLException {
try (PreparedStatement stmt = conn.prepareStatement(
"SELECT * FROM myarticles WHERE artid = ?")) {
stmt.setInt(1, id);
try (ResultSet resultset = stmt.executeQuery()) {
if (resultset.next()) {
Article article = new Article();
article.setArtid(resultset.getInt("artid"));
article.setTitle(resultset.getString("title"));
article.setBody(resultset.getString("body"));
return article;
} else {
return null;
}
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try (Connection conn = DBConnectionUtils.getConnectionFromSrcFolder("connection.properties")) {
Article articleid = ArticleDAO.articleById(id, conn);
req.setAttribute("articlebyid", articleid);
req.getRequestDispatcher("WEB-INF/content-page.jsp").forward(req, resp);
} catch (SQLException e) {
System.out.println("error " + e.getSQLState());
}
}
在单击包含文章ID的超链接时,我要显示文章的标题和正文
<c:forEach var="articles" items="${article}">
<tr>
<td>${articles.artid}</td>
<td><a href="./article-content?id=${articles.artid}">${articles.title}</a></td>
</tr>
</c:forEach>
<div class="container">
<h1>Article title goes here</h1>
${articlebyid.title}
<p>Article body goes here</p>
${articlebyid.body}
</div>
I forget to include the doget method that display the list of article title that has the hyperlink to display the body and title of the article. here
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try (Connection conn = DBConnectionUtils.getConnectionFromSrcFolder("connection.properties")) {
List<Article> allarticle =ArticleDAO.getAllArticles(conn);
req.setAttribute("article",allarticle);
req.getRequestDispatcher("WEB-INF/article-list-page.jsp").forward(req,resp);
}catch (SQLException e){
System.out.println("error "+ e.getSQLState());
}
}