jsp不使用框架实现分页(首页、下一页、上一页、末页)代码
时间: 2023-05-30 18:07:12 浏览: 115
JSP实现分页的代码
以下是一个基本的JSP实现分页的示例,没有使用任何框架:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%
// 设置页码和每页显示的记录数
int pageSize = 10;
int pageNum = 1;
String pageNumString = request.getParameter("pageNum");
if (pageNumString != null && !pageNumString.equals("")) {
pageNum = Integer.parseInt(pageNumString);
}
// 获取数据集合(这里假设是一个List)
List<String> dataList = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
dataList.add("数据" + (i + 1));
}
// 计算总页数
int totalCount = dataList.size();
int totalPage = (totalCount + pageSize - 1) / pageSize;
// 获取当前页的数据
List<String> currentPageData = new ArrayList<String>();
int startIndex = (pageNum - 1) * pageSize;
int endIndex = startIndex + pageSize;
if (endIndex > totalCount) {
endIndex = totalCount;
}
if (startIndex <= endIndex) {
currentPageData = dataList.subList(startIndex, endIndex);
}
// 输出分页数据
out.println("<table>");
for (String data : currentPageData) {
out.println("<tr><td>" + data + "</td></tr>");
}
out.println("</table>");
// 输出分页导航栏
out.println("<div>");
out.println("<a href=\"?pageNum=1\">首页</a>");
if (pageNum > 1) {
out.println("<a href=\"?pageNum=" + (pageNum - 1) + "\">上一页</a>");
}
if (pageNum < totalPage) {
out.println("<a href=\"?pageNum=" + (pageNum + 1) + "\">下一页</a>");
}
out.println("<a href=\"?pageNum=" + totalPage + "\">末页</a>");
out.println("</div>");
%>
阅读全文