简体   繁体   English

如何使用servlet和jstl在jsp中每页显示特定数量的结果

[英]How to do display a specific number of result per page in jsp using servlet and jstl

would like to get a working example of how to do greedy pagination in jsp. 想获得一个如何在jsp中进行贪婪分页的工作示例。 have done something but mine only displays 1 result. 做过什么,但我的只显示1个结果。

 jsp codes <table id="menDisplayTable"> <tr id="menTr"> <c:forEach items="${returnedMenWatches}" var="proDetails"> <td id="menTd"><img src="data:image/jpg;base64,${proDetails.mode4}" height="50" width="50"/><br> ${proDetails.brand}<br> ${proDetails.name}<br> ${proDetails.gender}<br> ${proDetails.price} </td> </c:forEach> </tr> </table> 

 servlet codes int page = 1; int recordsPerPage = 10; if(request.getParameter("page") !=null) page = Integer.parseInt(request.getParameter("page")); List<Product> returnedMenWatches = connect.getMenWatch(); int noOfRecords = connect.getNoOfRecords(); int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage); request.setAttribute("returnedMenWatches", returnedMenWatches); request.setAttribute("noOfPages", noOfPages); request.setAttribute("currentPage", page); request.getRequestDispatcher("men.jsp").forward(request, response); 

 DAO class static int noOfRecords; //method to display men products public List<Product> getMenWatch() throws SQLException { List<Product> returnedMenWatches = new ArrayList<Product>(); String male = "Male"; Statement myStatement = getConnection(); String query = "SELECT Product_ID, Brand, Product_Name, Gender, Product_Price, Product_Picture_Main, COUNT(Product_ID) AS NoOfRecords " + "FROM Products WHERE Gender = '"+male+"'"; rs = myStatement.executeQuery(query); while (rs.next()){ Product proDetails = new Product(); proDetails.setProductId(rs.getInt("Product_ID")); proDetails.setBrand(rs.getString("Brand")); proDetails.setName(rs.getString("Product_Name")); proDetails.setGender(rs.getString("Gender")); proDetails.setPrice(rs.getFloat("Product_Price")); Blob mode1 = rs.getBlob("Product_Picture_Main"); Blob mode2 = (Blob) mode1; byte[]mode3 = mode2.getBytes(1, (int)mode2.length()); String mode4 = Base64.encode(mode3); proDetails.setMode4(mode4); noOfRecords = rs.getInt("NoOfRecords"); returnedMenWatches.add(proDetails); } return returnedMenWatches; } // method to get number of records returned from the get men watches query public int getNoOfRecords() { return noOfRecords; } 

please i need your help, thanks in advance. 请我需要您的帮助,在此先感谢。 have don some editing, i added the DAO code where sql query is. 进行了一些编辑,我在sql查询所在的位置添加了DAO代码。

我认为您应该在<tr>标记之前放置<c:foreach....以重复表行。

Change your jsp code like this to add <tr> tag in each iteration of your forEach . 像这样更改您的jsp代码,以便在forEach每次迭代中添加<tr>标记。

<table id="menDisplayTable">    


                <c:forEach items="${returnedMenWatches}" var="proDetails">
                <tr>
                <td id="menTd"><img src="data:image/jpg;base64,${proDetails.mode4}" height="50" width="50"/><br>
                                ${proDetails.brand}<br>
                                ${proDetails.name}<br>
                                ${proDetails.gender}<br>
                                ${proDetails.price}
                 </td>
                 </tr>
                 </c:forEach> 


            </table>

It should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM