×

jsp分页

JSP页面分页怎么做?jsp 如何将查询结果实现分页,最好简单易懂…

admin admin 发表于2022-05-24 01:38:38 浏览109 评论0

抢沙发发表评论

JSP页面分页怎么做


分页须知知识点:
(1)JDBC2.0的可滚动结果集。
(2)HTTP GET请求。
一、可滚动结果集
Connection con = DriverManager.getConnection();
PreparedStatement stmt = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery();
常用方法:
(1)rs.absolute(n); 可以将指针跳到第n行。
(2)rs.relative(n); 可以将指针相对向下或向上n行。
(3)rs.first();
(4)rs.last();
(5)int curRow = rs.getRow(); 指针指向的当前行
二、功能实现分解
1.计算结果的个数
rs.last();
int size = rs.getRow();
即可得到结果的个数。

2.得到需要分几页
如果一页能够放5条记录,则
int pageCount = (size%5==0)?(size/5):(size/5+1);
即可获得需要分几页。

3.控制一页中规定显示记录个数
如果一页能显示5条记录,可以通过使用count进行计数。
int count = 0;
do{
if(count》=5) break;
.....
count++;
}while(rs.next());
通过break语句,能够使其显示到超过规定条目就跳出。

4.如何知道当前是第几页
通过HTTP get的特点,在地址栏中标明当前地址,如
《%@ page contentType=“text/html“ pageEncoding=“GB2312“ language=“java“%》
《%@ page import=“java.sql.*“%》
《html》
《head》
《title》hello《/title》
《/head》
《body》
《table border=“1“ spacing=“2“》
《%!
public static final String DRIVER = “com.mysql.jdbc.Driver“;
public static final String USER = “root“;
public static final String PASS = “12345“;
public static final String URL = “jdbc:mysql://localhost:3306/MLDN“;
public static final int PAGESIZE = 5;
int pageCount;
int curPage = 1;
%》
《%
//一页放5个
String user = null;
String pass = null;
try{
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL,USER,PASS);
String sql = “SELECT empno,ename,job,hiredate,sal,comm FROM emp“;
PreparedStatement stat = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stat.executeQuery();
rs.last();
int size = rs.getRow();
pageCount = (size%PAGESIZE==0)?(size/PAGESIZE):(size/PAGESIZE+1);
String tmp = request.getParameter(“curPage“);
if(tmp==null){
tmp=“1“;
}
curPage = Integer.parseInt(tmp);
if(curPage》=pageCount) curPage = pageCount;
boolean flag = rs.absolute((curPage-1)*PAGESIZE+1);
out.println(curPage);
int count = 0;

do{
if(count》=PAGESIZE)break;
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
Date hiredate = rs.getDate(4);
float sal = rs.getFloat(5);
int comm = rs.getInt(6);
count++;
%》
《tr》
《td》《%=empno%》《/td》
《td》《%=ename%》《/td》
《td》《%=job%》《/td》
《td》《%=hiredate%》《/td》
《td》《%=sal%》《/td》
《td》《%=comm%》《/td》
《/tr》
《%
}while(rs.next());
con.close();
}
catch(Exception e){

}
%》
《/table》
《a href = “multipage.jsp?curPage=1“ 》首页《/a》
《a href = “multipage.jsp?curPage=《%=curPage-1%》“ 》上一页《/a》
《a href = “multipage.jsp?curPage=《%=curPage+1%》“ 》下一页《/a》
《a href = “multipage.jsp?curPage=《%=pageCount%》“ 》尾页《/a》
第《%=curPage%》页/共《%=pageCount%》页
《/body》
《/html》

jsp 如何将查询结果实现分页,最好简单易懂…


jsp中分页最快捷的办法是用分页组件:
分页组件代码使用taglib实现的:
《%@ tag language=“java“ pageEncoding=“UTF-8“%》
《%@ taglib uri=“/WEB-INF/tld/c.tld“ prefix=“c“%》
《%@ attribute name=“curIndex“ type=“java.lang.Long“ required=“true“%》
《%@ attribute name=“pageSize“ type=“java.lang.Long“ required=“true“%》
《%@ attribute name=“pagerRange“ type=“java.lang.Long“ required=“true“%》
《%@ attribute name=“totalPage“ type=“java.lang.Long“ required=“true“%》
《%@ attribute name=“formId“ type=“java.lang.String“ required=“true“%》
《%
long begin = Math.max(1, curIndex - pagerRange/2);
long end = Math.min(begin + (pagerRange-1),totalPage);

request.setAttribute(“p_begin“, begin);
request.setAttribute(“p_end“, end);
%》
《table class=“pager“》
《tr》
《% if (curIndex!=1){%》
《td》《a href=“javascript:gotoPage(1)“》首页《/a》《/td》
《td》《a href=“javascript:gotoPage(《%=curIndex-1%》)“》上一页《/a》《/td》
《%}else{%》
《td class=“disabled“》《a href=“#“》首页《/a》《/td》
《td class=“disabled“》《a href=“#“》上一页《/a》《/td》
《%}%》

《c:forEach var=“i“ begin=“${p_begin}“ end=“${p_end}“》
《c:choose》
《c:when test=“${i == curIndex}“》
《td class=“active“》《a href=“#“》${i}《/a》《/td》
《/c:when》
《c:otherwise》
《td》《a href=“javascript:gotoPage(${i})“》${i}《/a》《/td》
《/c:otherwise》
《/c:choose》
《/c:forEach》

《% if (curIndex!=totalPage){%》
《td》《a href=“#“》下一页《/a》《/td》
《td》《a href=“#“》末页《/a》《/td》
《%}else{%》
《td class=“disabled“》《a href=“javascript:gotoPage(《%=curIndex+1%》)“》下一页《/a》《/td》
《td class=“disabled“》《a href=“javascript:gotoPage(《%=totalPage%》)“》末页《/a》《/td》
《%}%》
《td》《a》共${totalPage}页《/a》《/td》
《td class=“input_li“》跳转到:《input type=“text“ id=“p_pageIndex“ size=“2“ value=“《c:out value=“${pageIndex}“/》“/》页 《input type=“button“ id=“gotoBtn“ onclick=“gotoPageByBtn()“ value=“GO“/》《/td》
《td class=“input_li“》 每页:
《select id=“p_pageSizeSelect“ onchange=“gotoPage(《%=curIndex%》)“》
《option value=“10“ 《c:if test=“${pageSize==10}“》selected《/c:if》》10条《/option》
《option value=“20“ 《c:if test=“${pageSize==20}“》selected《/c:if》》20条《/option》
《option value=“50“ 《c:if test=“${pageSize==50}“》selected《/c:if》》50条《/option》
《/select》
《/td》
《/tr》
《/table》
jsp中使用方法:

《%@ taglib uri=“/WEB-INF/tld/c.tld“ prefix=“c“%》
《%@ taglib uri=“/WEB-INF/tld/fmt.tld“ prefix=“fmt“%》
《%@ taglib tagdir=“/WEB-INF/tags“ prefix=“tags“%》
《head》
《style》《!--分页样式--》
.pager { font: 12px Arial, Helvetica, sans-serif;}
.pager a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px;line-height:30px;vertical-align:middle;}
.pager .active a{color:red;border:none;}
.pager a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.pager a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}
.pager .input_li{padding: 1px 6px;}
《/style》
《script》《!--分页跳转脚本--》
function gotoPage(pageIndex){
var queryForm = document.getElementById(“queryForm“);
var action = queryForm.action;
var pageSize = document.getElementById(“p_pageSizeSelect“).value;
action += “?pageIndex=“ + pageIndex + “&pageSize=“ + pageSize;
//alert(action);
queryForm.action = action;
queryForm.submit();
}

function gotoPageByBtn(){
var pageIndex = document.getElementById(“p_pageIndex“).value;
var pageIndexInt = parseInt(pageIndex);
var totalPage = ${totalPage};

if(pageIndexInt》0 && pageIndexInt《totalPage){
gotoPage(pageIndex);
}
else{
alert(“输入页数超出范围!“);
}
}
《/script》
《/head》
《body》
《form id=“queryForm“ action=“${basePath}/log/list“ method=“post“》
《table》
《tr》
《td》用户名:《/td》
《td》《input type=“text“ name=“userName“ value=“《c:out value=“${userName}“/》“/》 《/td》
《td》《input type=“submit“ text=“查询“/》《/td》
《/tr》
《/table》
《/form》
《tags:pager pagerRange=“10“ pageSize=“${pageSize}“ totalPage=“${totalPage}“ curIndex=“${pageIndex}“ formId=“queryForm“》《/tags:pager》
《table class=“border“》
《thead》
《tr》
《th width=“100“》用户名称《/th》
《th width=“500“》操作内容《/th》
《th width=“200“》操作时间《/th》
《/tr》
《/thead》
《tbody》
《c:forEach items=“${logList}“ var=“log“》
《tr》
《td》${log.userName}《/td》
《td》${log.result}《/td》
《td》
《fmt:formatDate value=“${log.createTime}“ pattern=“yyyy-MM-dd HH:mm:ss“/》
《/td》
《/tr》
《/c:forEach》
《/tbody》
《/table》
《tags:pager pagerRange=“10“ pageSize=“${pageSize}“ totalPage=“${totalPage}“ curIndex=“${pageIndex}“ formId=“queryForm“》《/tags:pager》
《/body》

java的jsp如何分页显示查询结果


分页显示一般有两种实现方式:业务层分页、数据库层分页(以下会用到两个参数,提前说明下 page:请求第几页,size:每页显示多少条)

业务层分页:从数据库取出所有数据,然后通过传过来的page和size对所有数据截取,比如一共查了100条数据,保存在list里面,要求查询第2页,每页显示10条,则可以通过list属性,取100条数据 中的第11条到第20条,可通过遍历实现。-jsp分页

数据库层分页:数据库都会有分页函数(mysql 是limit函数,sqlServer是row_number()函数,可自行百度下)该方法是通过传过来的page和size在查询数据库时就开始分页,以mysql为例,查询第2页,每页显示10条,则sql语句是 ”select * from XX limit 10,10“(第一个10表示从下标为10开始查,第二个10是共读取10条)-jsp分页

性能肯定是第二种分页方式好,只要搞懂分页原理,想实现分页其实很简单,只要搞清楚分页是将多条数据中的某几条挑出来

Java

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。