简体   繁体   中英

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. have done something but mine only displays 1 result.

 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.

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

Change your jsp code like this to add <tr> tag in each iteration of your forEach .

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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