gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:35 25 4
gpt4 key购买 nike

想要一个如何在 jsp 中进行贪婪分页的工作示例。已经做了一些事情,但我的只显示 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;
}

请我需要你的帮助,提前致谢。进行了一些编辑,我添加了 sql 查询所在的 DAO 代码。

最佳答案

我认为你应该输入 <c:foreach....之前<tr>标记重复表行。

关于java - 如何使用 servlet 和 jSTL 在 jsp 中显示每页特定数量的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com