❶ 實現html頁面的分頁查詢,請問如何用jquery
動態的Ajax分頁,代碼如下:
<%@pagelanguage="java"contentType="text/html;charset=utf-8"
pageEncoding="utf-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<basehref="<%=basePath%>"/>
<metahttp-equiv="Content-Type"
content="text/html;charset=ISO-8859-1">
<title>Inserttitlehere</title>
<scripttype="text/javascript"src="scripts/jquery-1.5.1.js"></script>
<scripttype="text/javascript">
varlist;//thelistofdata
vartotalPages;//thetotalofpages
varpageSize=5;//eachsizeofpage
varpageIndex=1;//theindexofcurrentpage
$(function(){
send();
});
functionsend(){
$.ajax({
url:"DistrictServlet",
type:"POST",
data:{"function":"list"},
dataType:"json",
success:function(data){
//list=data;
varrecords=data.length;
if(records%pageSize==0){
totalPages=records/pageSize;
}else{
totalPages=Math.round(records/pageSize);
}
$("#pageIndex").html(pageIndex);
$("#totalPages").html(totalPages);
binding(data);
}
});
}
functionchangePage(){
$.ajax({
url:"DistrictServlet",
type:"post",
data:{"function":"list"},
dataType:"json",
success:function(data){
binding(data);
}
});
}
functionbinding(data){
varstart=(pageIndex-1)*pageSize;
varend=pageIndex*pageSize;
varhtml="";
$.each(data,function(index,district){
if(index>=start&&index<end){
//showdata
html+="<tr><td>"+district["id"]+"</td><td>"+district["name"]+"</td></tr>";//.........
}
});
$("table").html(html);
$("#pageIndex").html(pageIndex);
}
functionnextPage(){
pageIndex+=1;
if(pageIndex>totalPages){
pageIndex=totalPages;
return;
}
changePage();
}
functionlastPage(){
pageIndex-=1;
if(pageIndex<1){
pageIndex=1;
return;
}
changePage();
}
functionskipPage(index){
pageIndex=index;
changePage();
}
</script>
</head>
<body>
<div><spanid="pageIndex"></span>/<spanid="totalPages"></span></div>
<div>
<ahref="javascript:lastPage();">last</a>
<ahref="javascript:nextPage();">next</a>
</div>
<divid="list"><table></table></div>
</body>
</html>
❷ 按照thinkphp數據分頁寫完後前端該怎麼寫
一、分頁方法
/**
 * TODO 基礎分頁的相同代碼封裝,使前台的代碼更少
 * @param $m 模型,引用傳遞
 * @param $where 查詢條件
 * @param int $pagesize 每頁查詢條數
 * @return \Think\Page
 */
function getpage(&$m,$where,$pagesize=10){
    $m1=clone $m;//淺復制一個模型
    $count = $m->where($where)->count();//連慣操作後會對join等操作進行重置
    $m=$m1;//為保持在為定的連慣操作,淺復制一個模型
    $p=new Think\Page($count,$pagesize);
    $p->lastSuffix=false;
    $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>條記錄  每頁<b>%LIST_ROW%</b>條  第<b>%NOW_PAGE%</b>頁/共<b>%TOTAL_PAGE%</b>頁</li>');
    $p->setConfig('prev','上一頁');
    $p->setConfig('next','下一頁');
    $p->setConfig('last','末頁');
    $p->setConfig('first','首頁');
    $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
    $p->parameter=I('get.');
    $m->limit($p->firstRow,$p->listRows);
    return $p;
}
getpage方法可以放在TP框架的 Application/Common/Common/function.php,這個文檔可以專門放置一些通用的方法,在哪裡都可以調用(如:Controller文件,View文件等)。
 
二、調用分頁方法
$m=M('procts');
$p=getpage($m,$where,10);
$list=$m->field(true)->where($where)->order('id desc')->select();
$this->list=$list;
$this->page=$p->show();
再是View代碼
<div class="pagination">
{$page}
</div>
三、最後就是分頁的樣式了,這個有些亂,因後台框架網上下載的,樣式還沒來的及整理,這個樣式也可以自己實現,簡單的。
.pagination ul {
    display: inline-block;
    margin-bottom: 0;
    margin-left: 0;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.pagination ul li {
  display: inline;
}
.pagination ul li.rows {
    line-height: 30px;
    padding-left: 5px;
}
.pagination ul li.rows b{color: #f00}
.pagination ul li a, .pagination ul li span {
    float: left;
    padding: 4px 12px;
    line-height: 20px;
    text-decoration: none;
    background-color: #fff;
    background: url('../images/bottom_bg.png') 0px 0px;
    border: 1px solid #d3dbde;
    /*border-left-width: 0;*/
    margin-left: 2px;
    color: #08c;
}
.pagination ul li a:hover{
    color: red;
    background: #0088cc;
}
.pagination ul li.first-child a, .pagination ul li.first-child span {
    border-left-width: 1px;
    -webkit-border-bottom-left-radius: 3px;
    border-bottom-left-radius: 3px;
    -webkit-border-top-left-radius: 3px;
    border-top-left-radius: 3px;
    -moz-border-radius-bottomleft: 3px;
    -moz-border-radius-topleft: 3px;
}
.pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover {
color: #999;
cursor: default;
background-color: transparent;
}
.pagination ul .active a, .pagination ul .active span {
color: #999;
cursor: default;
}
.pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span {
background-color: #f0c040;
}
.pagination ul li.last-child a, .pagination ul li.last-child span {
    -webkit-border-top-right-radius: 3px;
    border-top-right-radius: 3px;
    -webkit-border-bottom-right-radius: 3px;
    border-bottom-right-radius: 3px;
    -moz-border-radius-topright: 3px;
    -moz-border-radius-bottomright: 3px;
}
.pagination ul li.current a{color: #f00 ;font-weight: bold; background: #ddd}
❸ html簡單的分頁代碼怎麼寫
網頁鏈接
看一下這個吧,現在很少有人手動寫分頁了,一般都是用插件。或者現在主流的前端框架,都有用戶量特別大的前端組件庫,用起來很方便。其實這個分頁手寫js並不難,主要是理清邏輯就可以了,能寫但是沒必要~如果是比較老的前端框架,必須手寫js分頁邏輯,追問就行,我給你屢屢
❹ 急求JSP的分頁顯示的代碼和詳細步驟
首先要定義四個變數:
int pageSize:每頁顯示多少條記錄
int pageNow:希望顯示第幾頁
int pageCount:一共有多少頁
int rowCount:一共有多少條記錄
說明:
pageSize是指定的 pageNow是用戶選擇的
rowCount是計算出來的 該計算式為
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
(技巧:
資料庫插入:
insert into 表名(欄位1,2,。。。)select 欄位1,2,...from 表名
)
查詢語句
select top pageSize欄位名列表from表名where id not in
(select top pageSize*(pageNow-1)id from 表名)
以我們前面的users表為例,顯示第二頁,該查詢語句就是:
select top 3 * from users where userId not in(select top 3 userId from users)
(select top 3 userId from users):選出這個表的前三條 前面再選三條
<h1>用戶信息列表</h1>
<%
//定義四個分頁會用到的變數
int pageSize=3;
int pageNow=1;//默認顯示第一頁
int rowCount=0;//該值從資料庫中查詢
int pageCount=0;//該值是通過pageSize和rowCount
//接受用戶希望顯示的頁數(pageNow)
String s_pageNow=request.getParameter("pageNow");
if(s_pageNow!=null){
//接收到了pageNow
pageNow=Integer.parseInt(s_pageNow);
}
//查詢得到rowCount
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;dataBaseName=System","sa","");
Statement sm=ct.createStatement();
ResultSet rs=sm.exeuteQuery("select count(*) form users ");
if(rs.next()){
rowCount=rs.getInt(1);
}
//計算pageCount
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
//查詢出需要顯示的記錄
rs=sm.exeuteQuery("select top "+pageSize
+" * from users where userId not in(select top "
+pageSize*(pageNow-1)+" userId from users) ");
%>
//顯示
<table border="1">
<tr><td>用戶ID</td><td>用戶名字</td><td>密碼</td><td>電郵</td><td>級別</td></tr>
<%
while(rs.next()){
%>
<tr><td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td><td><%=rs.getString(4)%></td><td><%=rs.getInt(5)%></td></tr>
<%}%>
</table>
<%
//上一頁
if(pageNow!=1){
out.println("<a href=wel.jsp?pageNow="+(pageNow-1)+">上一頁</a>");
}
//顯示超鏈接
for(int i=1;i<=pageCount;i++){
out.println("<a href=wel.jsp?pageNow="+i+">["+i+"]</a>");
}
//下一頁
if(pageNow!=pageCount){
out.println("<a href=wel.jsp?pageNow="+(pageNow+1)+">下一頁</a>");
}
%>
❺ 求jsp頁面實現分頁顯示資料庫查詢內容 代碼
這個是有條件查詢的分頁 
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*" %>
<%@page import="entity.Stu"%>
<script>
</script>
<html>
	<title>分頁顯示(jsp)</title>
	<body>
		<%! 
			int pageSize = 5;//每頁顯示的記錄數 
		    int pageCount = 0;//總頁數%>
		<%
			int ageS=0;//開始年齡
			int ageE=0;//結束年齡
			if(request.getParameter("ageS")==null){//判斷request里是否有ageS,沒有:給ageS賦值並把其放到session里
				ageS=18;
				session.setAttribute("ageS",ageS);
			}else{										//有的話,再對session里的進行判斷
				if(!session.getAttribute("ageS").equals(request.getParameter("ageS"))){ //如果session里的值和request里的值不同
					ageS=Integer.parseInt(request.getParameter("ageS").toString());//那麼把request里的值賦值給ageS,並放進session里
					session.setAttribute("ageS",request.getParameter("ageS"));
				}else{
					ageS=Integer.parseInt(session.getAttribute("ageS").toString());//否則 ageS直接從session里讀取
				}
			}
			if(request.getParameter("ageE")==null){
				ageE=25;
				session.setAttribute("ageE",ageE);
			}else{
				if(!session.getAttribute("ageE").equals(request.getParameter("ageE"))){
					ageE=Integer.parseInt(request.getParameter("ageE").toString());
					session.setAttribute("ageE",request.getParameter("ageE"));
				}else{
					ageE=Integer.parseInt(session.getAttribute("ageE").toString());
				}
			}
			Connection conn = null;
			String driver = "oracle.jdbc.driver.OracleDriver";
			String url = "jdbc:oracle:thin:@localhost:1521:orcl";
			try {
				Class.forName(driver);
				conn = DriverManager.getConnection(url, "system", "zhu");
				PreparedStatement st=conn.prepareStatement("select * from stu where age>="+ageS+" and age<= "+ageE+" order by sid",
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_READ_ONLY);//可滾動查詢數據的結果集
				ResultSet rs = st.executeQuery();
				
				rs.last();//讓游標到表中的最後一行
				int rowCount = rs.getRow();//獲取記錄總數
				//out.print("記錄總數為 :"+rowCount);
				//總也數的計算公式
				pageCount = (rowCount % pageSize == 0) ? (rowCount / pageSize)
						: (rowCount / pageSize + 1);
				int showPage = 1;//當前頁
		%>
		<%
			//取得用戶指定的頁
				String goTOPage = request.getParameter("showPage");
				if (goTOPage == null) {
					goTOPage = "1";
				}
				try {
					showPage = Integer.parseInt(goTOPage);
				} catch (NumberFormatException e) {
					showPage = 1;
				}
				//當頁面小於等於第一頁。按第一頁算 如果大於等於總頁數。按最後一頁算
				if (showPage <= 1) {
					showPage = 1;
				} else if (showPage >= pageCount) {
					showPage = pageCount;
				}
				//游標的位置(當前頁-1)×每頁顯示的記錄數+1
				int posion = (showPage - 1) * pageSize + 1;
				//設置游標的位置
				rs.absolute(posion);
				
		%>
		<form action="" method="get">    年齡段:<input type="text" value="<%=ageS %>"  name="ageS"/>——<input type="text" value="<%=ageE %>"  name="ageE"/> 
			<input type="submit" value="查詢"/>
		
		<table border="1" cellpadding="0" cellspacing="0">
			<tr>
				<td>序號</td>
				<td>
					編號
				</td>
				<td>
					姓名
				</td>
				<td>
					年齡
				</td>
			</tr>
			<%
				int num=showPage*pageSize-4;
				int i = 0;
					//循環顯示表中的數據pageSize(每頁顯示的記錄)
					//rs.isAfterLase()游標是否在最後一行之後 說明後面已經沒有記錄
					while (i < pageSize && !rs.isAfterLast()) {
			%>
			<tr>
				<td>
					<%=num %>
				</td>
				<td>
					<%=rs.getInt("sid") %>
				</td>
				<td>
					<%=rs.getString("sname") %>
				</td>
				<td><%=rs.getInt("age") %>
				</td>
			</tr>
			<%
				rs.next();
						i++;
						num++;
					}
			%>
		</table>
			<table width="1000">
				<tr style=" vertical-align:middle;">
					<td>
						<a href="page1.jsp?ageS=<%=ageS %>&ageE=<%=ageE %>&showPage=1">首頁</a>
					</td>
					
					<td>
						<%
							if (showPage > 1) {
						%>
						<a href="page1.jsp?ageS=<%=ageS %>&ageE=<%=ageE %>&showPage=<%=showPage - 1%>">上一頁</a>
						<%
							}else{
						%>
							上一頁
						<%} %>
					</td>
					<td>
						<%
							if (showPage < pageCount) {
						%>
						<a href="page1.jsp?ageS=<%=ageS %>&ageE=<%=ageE %>&showPage=<%=showPage + 1%>">下一頁</a>
						<%
							}else{
						%>
							下一頁
						<%} %>
					</td>
					<td>
						<a href="page1.jsp?ageS=<%=ageS %>&ageE=<%=ageE %>&showPage=<%=pageCount%>">尾頁</a>
					</td>
					<td>
						共<%=pageCount%>頁
					</td>
					<td>
						第<%=showPage%>頁
					</td>
				</tr>
			</table>
		</form>
		<%
			conn.close();
			} catch (ClassNotFoundException e1) {
				out.print(e1.getMessage());
			} catch (SQLException e2) {
				out.print(e2.getMessage());
			}
		%>
	</body>
</html>
❻ 在同一頁面中如何實現查詢並分頁顯示結果
建立access的資料庫news,還有表news,表的欄位(id,title),id唯一,輸入數據保存,用下面代碼可查詢,可分頁 
-----------------------下面保存為search.asp-------------------------- 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title>文件</title> 
</head> 
<body bgcolor="#ffffff"> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<script> 
function btn_ck_bh_Click() 
{ 
var cx = document.form1.cxsj.value; 
form1.action ="search.asp?cx="+cx; 
} 
</script> 
<table border="1" cellspacing="0" bgcolor="#F0F8FF" bordercolorlight="#4DA6FF" bordercolordark="#ECF5FF" width="88%" style="word-break:break-all"> 
<tr> 
<td width="778" align="center" colspan="7"> 
<form method="POST" name="form1" action=search.asp> 
<p>輸入搜索內容:<input type="text" name="cxsj" size="20"><input type="submit" value="提交" name="B1" LANGUAGE="javascript" onclick="btn_ck_bh_Click()"> 
<input type="reset" value="重寫" name="B2"></p> 
</form> 
</td> 
</tr> 
</table> 
<table border="1" cellspacing="0" bgcolor="#F0F8FF" bordercolorlight="#4DA6FF" bordercolordark="#ECF5FF" width="88%" style="word-break:break-all"> 
<tr> 
<td width="8%" align="center"><strong><font color="#0080C0">ID 號</font></strong></td> 
<td width="58%" align="center"><strong><font color="#0080C0">標 題</font></strong></td> 
<td width="8%" align="center"><strong><font color="#0080C0">修 改</font></strong></td> 
<td width="8%" align="center"><strong><font color="#0080C0">刪 除</font></strong></td> 
</tr> 
<% 
'資料庫查詢 
'獲得搜索內容 
cx = request("cx") 
dim pageCount 
'把page轉換成整數 
page = cint(request("page")) 
set conn=server.createobject("adodb.connection")' 
set rs=server.createobject("adodb.recordset") 
conn.open "DBQ=" & server.mappath("./news.mdb") & ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};" 
' 獲取產品的名字記錄集(從 news表中) 
if cx <> "" then 
sql = "select * from news where title like '%"&cx& "%' order by id desc" 
else 
sql ="select * from news order by id desc" 
end if 
rs.open sql,conn,3,3 
'如果沒有數據記錄 
if rs.bof then 
errmsg=errmsg+"<br>"+"<li>"+keyword+"沒有記錄,請返回!!" 
response.write errmsg 
response.end 
end if 
' 設置記錄集在每頁的總行數,也就是 PageSize屬性 
RS.PageSize=40 
'把rs.pageCount轉換成整數和page才能作比較 
pageCount = cint(rs.pageCount) 
' 設置當前的頁號( AbsolutePage屬性) 
if page = 0 then 
page =1 
end if 
RS.AbsolutePage = page 
x=1 
' 顯示當前頁中的所有記錄( PageSize中設置的行數) 
WHILE NOT RS.EOF AND NumRows<RS.PageSize 
%> 
<tr onmouseover="this.bgColor='#99ccff'" onmouseout="this.bgColor=''"> 
<td width="8%"><p align="center"><%=rs("id")%></td> 
<td width="58%"><a href="view.asp?id=<%=rs("id")%>" target="_blank"><%=rs("title")%></a></td> 
<td width="8%" align="center"><a href="edit.asp?id="<%=rs("id")%>>修 改</a></td> 
<td width="8%" align="center"><a href="delet.asp?id="<%=rs("id")%>>刪 除</a></td> 
</tr> 
<%RS.MoveNext 
NumRows=NumRows+1 
WEND%> 
<tr onmouseover="this.bgColor='#99ccff'" onmouseout="this.bgColor=''"> 
<td width="105%" align="center" colspan="6"> </td> </tr> 
<tr> 
<td width="105%" align="center" colspan="6"> 
<p align="center"><FONT color=#333333>共<%=PageCount%>頁 第<%=page%>頁★ 
<%if page=1 then%>首頁<%end if%> 
<%if page>1 then%> 
<A HREF="search.asp?page=1&cx=<%=cx%>"> 首頁</A> 
<%end if%>★ 
<%if page>1 then%><A HREF="search.asp?page=<%=page-1%>&cx=<%=cx%>"><%end if%>上一頁</a> 
<% 
dim pagewhere 
dim p 
p = 1 
'把pagewhere轉換成整數 
'pagewhere = cint(request("pagewhere")) 
pagewhere = pageCount 
if pagewhere>0 then 
for p=1 to pagewhere 
if p <> page then%> 
<A HREF="search.asp?page=<%=p%>&cx=<%=cx%>"><%=p%></a> 
<%end if 
if p =page then%> 
<%=p%> 
<% end if 
next 
end if%> 
<%if page < PageCount then%> 
<A HREF="search.asp?page=<%=page+1%>&cx=<%=cx%>"> 
<%end if %>下一頁</A>★ 
<%if page=PageCount then%>尾頁 
<%end if%> 
<%if page<PageCount then%> 
<A HREF="search.asp?page=<%=PageCount%>&cx=<%=cx%>"> 尾頁</A> 
<%end if%> 
</p></FONT></td> </tr> <tr> 
<td width="105%" align="center" colspan="6">搜索內容:<%=cx%></td> 
</tr> 
</table></center></div> 
</body></html> 
<% 
rs.close 
Set rs=nothing 
conn.close 
set conn=nothing 
%>
❼ 前端一下獲取所有數據怎麼實現分頁
grid的分頁功能本身就是對資料庫數據分頁後的一個數據顯示,並可以執行翻頁查詢其他頁的信息。全部信息則只需要將limit參數設置為數據總條數,pagesize也設置為數據行數。grid中不設置分頁組件是可以直接顯示全部信息,url中不設置limit和start參數。注:數據量太多時不宜全部顯示
❽ 前端開發 需要根據把後台數據列表 分頁顯示
搭建項目分頁
1.jsp頁面
<tr>
<td bgcolor="#00ffff" colspan="10" align="center">
第${pager.currentPage}頁
每頁${pager.pageSize} 條記錄
共${pager.totalPages}頁
共${pager.totalRows} 條
<a href="servlet/UserServlet?num=1&page=1" >首頁</a>
<a href="servlet/UserServlet?num=1&page=${pager.currentPage-1}">上一頁</a>
<a href="servlet/UserServlet?num=1&page=${pager.currentPage+1}">下一頁</a>
<a href="servlet/UserServlet?num=1&page=${pager.totalPages}">末頁</a>
</td>
</tr>
2.拼接sql語句
SELECT * FROM USERS LEFT JOIN DEPT ON USERDEPT=DEPTNUM
SELECT * FROM(SELECT A.*, ROWNUM RN FROM (SELECT * FROM student) A WHERE ROWNUM <=15)WHERE RN >=11 ORDER BY ENTERDATE
分頁sql:SELECT * FROM(SELECT A.*, ROWNUM RN FROM (SELECT * FROM USERS LEFT JOIN DEPT ON USERDEPT=DEPTNUM) A WHERE ROWNUM <=15)WHERE RN >=11 ORDER BY ENTERDATE
3.分頁工具類導入到common包:Pager.java
在servlet調用:第一個條件:總條數 第二個條件:當前頁數
String page = request.getParameter("page");
//分頁調用開始--------------------------------
Pager pager = new Pager();
int totalRows = userService.getCountRows();
pager.setTotalRows(totalRows);
if(null != page){
pager.setCurrentPage(Integer.parseInt(page));
}else{
pager.setCurrentPage(1);
}
request.setAttribute("pager", pager);//給頁面分頁賦值
4.把pager工具類傳遞到,拼接執行動態分頁sql語句
開始條數:pager.getStartRow()
結束條:pager.getCurrentPage()*pager.getPageSize()
String sql="SELECT * FROM(SELECT A.*, ROWNUM RN FROM (SELECT * FROM USERS LEFT JOIN DEPT ON USERDEPT=DEPTNUM) A WHERE ROWNUM <="+pager.getCurrentPage() *pager.getPageSize()+")WHERE RN >="+pager.getStartRow();//分頁sql 1-3條
分頁根本條件:
private int totalRows; //總行數
private int pageSize = 5; //每頁顯示的行數
private int currentPage=1; //當前頁號
private int totalPages; //總頁數
private int startRow; //當前頁在資料庫中的起始行
private int endRow; //資料庫執行sql的結束行
totalPages演算法:
if(totalRows<=pageSize){
totalPages=1;
}else{
totalPages=(totalRows+pageSize-1)/pageSize;
}
startRow; 演算法:
startRow=(currentPage-1) * pageSize+1;
endRow; endRow=currentPage* pageSize;
❾ 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如何分頁顯示查詢結果呢
//分頁類
publicclassPager{
privateintpageNow=1;//
privateintpageSize=7;//
privateinttotalPage;//
privateinttotalSize;//
publicPager(intpageNow,inttotalSize){
this.pageNow=pageNow;
this.totalSize=totalSize;
}
publicintgetPageNow(){
returnpageNow;
}
publicvoidsetPageNow(intpageNow){
this.pageNow=pageNow;
}
publicintgetPageSize(){
returnpageSize;
}
publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize;
}
publicintgetTotalPage(){
totalPage=getTotalSize()/getPageSize();
if(totalSize%pageSize!=0)
totalPage++;
returntotalPage;
}
publicvoidsetTotalPage(inttotalPage){
this.totalPage=totalPage;
}
publicintgetTotalSize(){
returntotalSize;
}
publicvoidsetTotalSize(inttotalSize){
this.totalSize=totalSize;
}
publicbooleanisHasFirst(){
if(pageNow==1)
returnfalse;
else
returntrue;
}
publicvoidsetHasFirst(booleanhasFirst){
}
publicbooleanisHasPre(){
if(this.isHasFirst())
returntrue;
else
returnfalse;
}
publicvoidsetHasPre(booleanhasPre){
}
publicbooleanisHasNext(){
if(isHasLast())
returntrue;
else
returnfalse;
}
publicvoidsetHasNext(booleanhasNext){
}
publicbooleanisHasLast(){
if(pageNow==this.getTotalPage())
returnfalse;
else
returntrue;
}
publicvoidsetHasLast(booleanhasLast){
}
}
//service層
publicclassPageService{
@SuppressWarnings("unchecked")
publicList<?>list(intpageNow,intpageSize,Stringhql){
Sessionsession=HibernateSessionFactory.getSession();
Transactiontx=session.beginTransaction();
List<Object>objects;
Queryquery=session.createQuery(hql);
query.setFirstResult(pageSize*(pageNow-1));
query.setMaxResults(pageSize);
objects=query.list();
tx.commit();
returnobjects;
}
}
//在action中調用
publicStringlistUser(){
Stringhql="fromUserinfou";
if(ps.list(pageNow,pageSize,hql)!=null){
userinfos=(List<Userinfo>)ps.list(pageNow,pageSize,hql);
Map<String,Object>request=(Map<String,Object>)ActionContext
.getContext().get("request");
Pagerpage=newPager(this.getPageNow(),us.getUserSize());
request.put("userinfos",userinfos);
request.put("page",page);
returnAction.SUCCESS;
}else{
returnAction.LOGIN;
}
}
//jsp中
<body>
<tablewidth="832"border="0"cellpadding="0"cellspacing="0"id="listBook">
<trbgcolor="#E7E7E9">
<tdwidth="5%"height="40"> </td>
<tdwidth="25%"colspan="2"bgcolor="#E7E7E9"><divalign="center"class="STYLE10">郵箱</div></td>
<tdwidth="25%"colspan="2"bgcolor="#E7E7E9"class="STYLE1"><divalign="center"class="STYLE10">密碼</div></td>
<tdwidth="25%"colspan="2"bgcolor="#E7E7E9"class="STYLE1"><divalign="center"class="STYLE10">許可權</div></td>
<tdwidth="8%"bgcolor="#E7E7E9"><spanclass="STYLE8"></span></td>
<tdwidth="8%"bgcolor="#E7E7E9"><spanclass="STYLE8"></span></td>
</tr>
<s:iteratorvalue="#request.userinfos"id="oneUser">
<tr>
<tdheight="50">
<divalign="center">
<inputtype="checkbox"name="checkbox"value="checkbox"/>
</div></td>
<tdwidth="5%"></td>
<tdwidth="23%"class="STYLE4"><s:property
value="#oneUser.email"/></td>
<tdwidth="5%"class="STYLE4"></td>
<tdwidth="23%"><spanclass="STYLE4"><s:property
value="#oneUser.password"/></span></td>
<tdwidth="5%"class="STYLE4"></td>
<tdwidth="23%"><spanclass="STYLE4">
<s:iftest="#oneUser.power==1">
普通用戶
</s:if>
<s:else>
管理員
</s:else>
</span></td>
<td><divalign="right"class="STYLE1"><ahref='deleteUser?userid=<s:propertyvalue="#oneUser.id"/>'class="STYLE5">刪除|</a></div></td>
<tdclass="STYLE1"><ahref='lookUser?userid=<s:propertyvalue="#oneUser.id"/>&pageNow=<s:propertyvalue="#request.page.pageNow"/>'target="_self"class="STYLE5">修改</a></td>
</tr>
</s:iterator>
<tr>
<tdcolspan="9"><tablewidth="832"border="0"cellspacing="0"bgcolor="#E7E7E9">
<s:setname="page"value="#request.page"></s:set>
<tr>
<tdwidth="70%"> </td>
<s:iftest="#page.isHasPre()">
<tdwidth="10%"><ahref='listUser?pageNow=<s:propertyvalue="#page.pageNow-1"/>'target="_self"class="STYLE3">上一頁</a></td>
</s:if>
<s:else>
<tdwidth="10%"><ahref="listUser?pageNow=1"target="_self"class="STYLE3">上一頁</a></td>
</s:else>
<s:iftest="#page.isHasNext()">
<tdwidth="10%"><ahref="listUser?pageNow=<s:propertyvalue="#page.pageNow+1"/>"target="_self"class="STYLE3">下一頁</a></td>
</s:if>
<s:else>
<tdwidth="10%"><ahref="listUser?pageNow=<s:propertyvalue="#page.totalPage"/>"target="_self"class="STYLE3">下一頁</a></td>
</s:else>
<tdwidth="10%"><ahref="listUser?pageNow=<s:propertyvalue="#page.totalPage"/>"target="_self"class="STYLE3">尾頁</a></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>這是採用struts2+hibernate 做的,你可以參考一下
