我试图将我的产品列表分成几页。每页包含12条记录。我使用servlet从jsp文件和JSTL中检索数据,以在JSP文件中显示数据。但是我的jsp文件无法显示任何内容。主要问题似乎是" productList"不适用于jsp 这是我的servlet。
public class ProductServlet extends HttpServlet{
private static final long serialVersionUID=1L;
public ProductServlet(){
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
int page=1;
int rowsPerPage=12;
if (request.getParameter("page") != null) {
page = Integer.parseInt(request.getParameter("page"));
}
ProductDAO pd = new ProductDAO();
List<Product> list = pd.getAllPaging((page - 1) * rowsPerPage, rowsPerPage);
int noOfRows = list.size();
int noOfPages = noOfRows / rowsPerPage;
request.setAttribute("productList", list);
request.setAttribute("noOfPages", noOfPages);
request.setAttribute("currentPage", page);
RequestDispatcher view = request.getRequestDispatcher("productlist.jsp");
view.forward(request, response);
}
}
这是我的productlist.jsp文件。
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<th>Game ID</th>
<th>Game name</th>
<th>price</th>
<th>Realease date</th>
</tr>
<c:forEach var="product" items="${productList}">
<tr>
<td><c:out value="${product.gameid}"/></td>
<td><c:out value="${product.gamename}"/></td>
<td><c:out value="${product.price}"/></td>
<td><c:out value="${product.releasedate}"/></td>
</tr>
</c:forEach>
</table>
<%--For displaying Previous link except for the 1st page --%>
<c:if test="${currentPage != 1}">
<td><a href="productlist.jsp?page=${currentPage - 1}">Previous</a></td>
</c:if>
<%--For displaying Page numbers.
The when condition does not display a link for the current page--%>
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<c:forEach begin="1" end="${noOfPages}" var="i">
<c:choose>
<c:when test="${currentPage eq i}">
<td>${i}</td>
</c:when>
<c:otherwise>
<td><a href="productlist.jsp?page=${i}">${i}</a></td>
</c:otherwise>
</c:choose>
</c:forEach>
</tr>
</table>
最后,我的web.xml
<servlet>
<servlet-name>ProductServlet</servlet-name>
<servlet-class>product.ProductServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProductServlet</servlet-name>
<url-pattern>/productlist</url-pattern>
</servlet-mapping>
我调试了我的方法getAllPaging()并且它工作得很好,我检查并使用JSTL语法重新检查了我的jsp。没关系。所以我认为问题属于我的servlet,但我无法弄清楚它是什么。谢谢你的帮助。