我将在jsp页面上打印列表中的项目。列表中有一个项目。 Servlet代码:
@WebServlet("/BlogListServlet")
public class BlogListServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Blog blog= (Blog) getServletContext().getAttribute("blog");
Map<Integer, Post> posts= blog.getPosts();
List<Post> list = new ArrayList<Post>(posts.values());
req.setAttribute("posts",list);
resp.sendRedirect("bloglist.jsp");
}
}
JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BlogPost</title>
<link rel="stylesheet" type="text/css" href="css/blogpost.css"/>
</head>
<body>
<table>
<c:forEach var="post" items="${posts}">
<tr>
<td>${post.name} - ${post.jaar}</td>
<td>${post.specialisatie}</td>
<td>${post.omschrijving}<br/><a href="${post.url}">${post.url}</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
导致浏览器:
$ {post.name} - $ {post.jaar} $ {post.specialisatie} $ {post.omschrijving} $ {post.url}