2019.02.27(java中的真假分页)
作者:互联网
真假分页
真分页
代码案例
SELECT * from user LIMIT 3,3
运行结果:
代码格式:
select * from user limit 起点,步长
记录的起点默认从0开始
假分页
第一步:
编写panger类,类中包含首页,尾页,总页数,总记录数,每页显示记录数,当前页
public class Panger {
int start;
int end;
int pangerNow;
int total;
int pageSize;
int sum;
public Panger(int start, int end, int pangerNow, int total, int pageSize,int sum) {
this.start = start;
this.end = end;
this.pangerNow = pangerNow;
this.total = total;
this.pageSize = pageSize;
this.sum = sum;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getPangerNow() {
return pangerNow;
}
public void setPangerNow(int pangerNow) {
this.pangerNow = pangerNow;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
第二步:编写前端界面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1px">
<tr>
<th>uid</th>
<th>username</th>
<th>age</th>
<th>hobby</th>
</tr>
<%--jsp循环展示数据--%>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.uid}</td>
<td>${user.username}</td>
<td>${user.age}</td>
<td>${user.hobby}</td>
</tr>
</c:forEach>
</table>
<%--上传和接收分页数据--%>
第${page.pangerNow}页/共${page.sum}页<a href="ds?pageNow=${page.start}">首页</a><a href="ds?pageNow=${page.pangerNow - 1}">上一页</a>${page.pangerNow}<a href="ds?pageNow=${page.pangerNow + 1}">下一页</a><a href="ds?pageNow=${page.end}">尾页</a>
</body>
</html>
第三步:编写servlet
public class DemoServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//查询数据库并接受数据
DBUtil db = new DBUtil();
String sql = "select * from user";
Object[] obj = {};
ResultSet rs = db.select(sql, obj);
List<User> list = new ArrayList<>();
try {
while (rs.next()){
int uid = rs.getInt(1);
String username = rs.getString(2);
int age = rs.getInt(3);
String hobby = rs.getString(4);
User user = new User(uid,username,age,hobby);
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
//接收前端数据,当前页面的显示页数
String pageNow = request.getParameter("pageNow");
//设置页面属性
int start = 1;
int total = list.size();
int pageSize = 3;
int sum;
if(total % pageSize == 0){
sum = total / pageSize;
}else{
sum = total / pageSize + 1;
}
int end = sum;
int pangerNow = 1;
if(pageNow != null && !pageNow.equals("")){
int pageNow1 = Integer.parseInt(pageNow);
if(pageNow1 < 1){
pangerNow = 1;
}else if(pageNow1 > end){
pangerNow = end;
}else{
pangerNow = pageNow1;
}
}
//设置每页显示的数据
List<User> list1 = new ArrayList<>();
for (int i = (pangerNow - 1)*pageSize; i < pangerNow*pageSize; i++) {
if(i == list.size()){
break;
}else{
list1.add(list.get(i));
}
}
Panger page = new Panger(start,end,pangerNow,total,pageSize,sum);
//传输数据,跳转页面
request.setAttribute("list",list1);
request.setAttribute("page",page);
request.getRequestDispatcher("index1.jsp").forward(request,response);
}
}
标签:pangerNow,27,java,pageSize,int,sum,2019.02,total,public 来源: https://blog.csdn.net/qq_34191426/article/details/88043751