java分页完整代码_JavaWeb中的简单分页完整代码(推荐)
时间: 2023-07-09 12:09:29 浏览: 114
JavaWeb中的简单分页完整代码(推荐)
以下是JavaWeb中的简单分页完整代码,使用JSP和Servlet实现:
1. 在JSP页面中,使用表格展示数据并添加分页导航:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分页示例</title>
</head>
<body>
<h1>分页示例</h1>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</table>
<br>
<c:if test="${page > 1}">
<a href="list?page=${page - 1}">上一页</a>
</c:if>
<c:if test="${page < pageCount}">
<a href="list?page=${page + 1}">下一页</a>
</c:if>
</body>
</html>
```
2. 在Servlet中查询数据库并设置分页相关参数:
```java
package com.example.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.dao.UserDao;
import com.example.entity.User;
@WebServlet("/list")
public class UserListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int pageSize = 10; // 每页显示的记录数
int page = Integer.parseInt(request.getParameter("page")); // 当前页码
int offset = (page - 1) * pageSize; // 当前页第一条记录的偏移量
UserDao dao = new UserDao();
List<User> list = dao.list(offset, pageSize); // 查询当前页的数据
int totalCount = dao.count(); // 总记录数
int pageCount = (int) Math.ceil(totalCount * 1.0 / pageSize); // 总页数
request.setAttribute("list", list);
request.setAttribute("page", page);
request.setAttribute("pageCount", pageCount);
request.getRequestDispatcher("list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
```
3. 在DAO中查询数据库并返回数据:
```java
package com.example.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.example.entity.User;
public class UserDao {
private String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private String username = "root";
private String password = "123456";
public List<User> list(int offset, int pageSize) {
List<User> list = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(url, username, password)) {
String sql = "SELECT * FROM user LIMIT ?, ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, offset);
stmt.setInt(2, pageSize);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
list.add(user);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public int count() {
int count = 0;
try (Connection conn = DriverManager.getConnection(url, username, password)) {
String sql = "SELECT COUNT(*) FROM user";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
count = rs.getInt(1);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
}
```
可以根据需求自行修改代码,例如更改每页显示的记录数、更改查询语句等。
阅读全文