jsp分页怎么做?

jsp分页怎么做?,第1张

分页有三种
一 用sql语句分页 每次查询的结果都是一页 也就是只查出每页需要的行数
二 用java等后台语句分页 查询的额时候把所有的内容都查询出来 放在一个数组中 然后分页时只取数组中的记录数
三 以上两种方法结合
区别在于 方法一每翻一页都要查询一次数据库 方法二只查询一次 这样的话对访问量比较小的网站 可以采用 如果访问量比较大 这样会影响数据库的性能 方法二只查一次 只适合数据量较小的查询 如果数据量较大 会占用较大的客户端资源 第三种方法比较适中 一般大型网站采用 具体根据实际情况而定吧 本人比价喜欢第一种 源码太多 就不附带了 有问题可以Q我

告诉我QQ油箱,然后我把分页的例子给你发过去!javabean连接SQLSEVER数据库的代码,是用myeclipse做的,有什么不懂的可以再问我,但是分请我给我拉!呵呵我要分!代码多所以不好发!

jsp中分页最快捷的办法是用分页组件:
分页组件代码使用taglib实现的:
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/ctld" prefix="c"%>
<%@ attribute name="curIndex" type="javalangLong" required="true"%>
<%@ attribute name="pageSize" type="javalangLong" required="true"%>
<%@ attribute name="pagerRange" type="javalangLong" required="true"%>
<%@ attribute name="totalPage" type="javalangLong" required="true"%>
<%@ attribute name="formId" type="javalangString" required="true"%>
<%
long begin = Mathmax(1, curIndex - pagerRange/2);
long end = Mathmin(begin + (pagerRange-1),totalPage);

requestsetAttribute("p_begin", begin);
requestsetAttribute("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/ctld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/fmttld" 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 = documentgetElementById("queryForm");
var action = queryFormaction;
var pageSize = documentgetElementById("p_pageSizeSelect")value;
action += "pageIndex=" + pageIndex + "&pageSize=" + pageSize;
//alert(action);
queryFormaction = action;
queryFormsubmit();
}
function gotoPageByBtn(){
var pageIndex = documentgetElementById("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>${loguserName}</td>
<td>${logresult}</td>
<td>
<fmt:formatDate value="${logcreateTime}" 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>

//分页类
public class Pager {
private int pageNow = 1;//
private int pageSize = 7;//
private int totalPage;//
private int totalSize;//
public Pager(int pageNow, int totalSize) {
thispageNow = pageNow;
thistotalSize = totalSize;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
thispageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
thispageSize = pageSize;
}
public int getTotalPage() {
totalPage = getTotalSize() / getPageSize();
if (totalSize % pageSize != 0)
totalPage++;
return totalPage;
}
public void setTotalPage(int totalPage) {
thistotalPage = totalPage;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
thistotalSize = totalSize;
}
public boolean isHasFirst() {
if (pageNow == 1)
return false;
else
return true;
}
public void setHasFirst(boolean hasFirst) {
}
public boolean isHasPre() {
if (thisisHasFirst())
return true;
else
return false;
}
public void setHasPre(boolean hasPre) {
}
public boolean isHasNext() {
if (isHasLast())
return true;
else
return false;
}
public void setHasNext(boolean hasNext) {
}
public boolean isHasLast() {
if (pageNow == thisgetTotalPage())
return false;
else
return true;
}
public void setHasLast(boolean hasLast) {
}
}
//service层
public class PageService {
@SuppressWarnings("unchecked")
public List<> list(int pageNow, int pageSize, String hql) {
Session session = HibernateSessionFactorygetSession();
Transaction tx = sessionbeginTransaction();
List<Object> objects;
Query query = sessioncreateQuery(hql);
querysetFirstResult(pageSize  (pageNow - 1));
querysetMaxResults(pageSize);
objects = querylist();
txcommit();
return objects;
}
}
//在action中调用public String listUser() {
String hql = "from Userinfo u";
if (pslist(pageNow, pageSize, hql) != null) {
userinfos = (List<Userinfo>) pslist(pageNow, pageSize, hql);
Map<String, Object> request = (Map<String, Object>) ActionContext
getContext()get("request");
Pager page = new Pager(thisgetPageNow(), usgetUserSize());
requestput("userinfos", userinfos);
requestput("page", page);
return ActionSUCCESS;
} else {
return ActionLOGIN;
}
}
//jsp中
<body>
<table width="832" border="0" cellpadding="0" cellspacing="0" id="listBook">
  <tr bgcolor="#E7E7E9">
    <td width="5%" height="40">&nbsp;</td>
    <td width="25%" colspan="2" bgcolor="#E7E7E9"><div align="center" class="STYLE10">邮箱</div></td>
    <td width="25%" colspan="2" bgcolor="#E7E7E9" class="STYLE1"><div align="center" class="STYLE10">密码</div></td>
    <td width="25%" colspan="2" bgcolor="#E7E7E9" class="STYLE1"><div align="center" class="STYLE10">权限</div></td>
    <td width="8%" bgcolor="#E7E7E9"><span class="STYLE8"></span></td>
    <td width="8%" bgcolor="#E7E7E9"><span class="STYLE8"></span></td>
  </tr>
  <s:iterator value="#requestuserinfos" id="oneUser">
  <tr>
    <td height="50">
    
      <div align="center">
        <input type="checkbox" name="checkbox" value="checkbox" />
       
    </div></td>
    <td width="5%"></td>
    <td width="23%" class="STYLE4"><s:property
value="#oneUseremail" /></td>
    <td width="5%" class="STYLE4"></td>
    <td width="23%"><span class="STYLE4"><s:property
value="#oneUserpassword" /></span></td>
    <td width="5%" class="STYLE4"></td>
    <td width="23%"><span class="STYLE4">
    <s:if test="#oneUserpower==1">
         普通用户
    </s:if>
    <s:else>
         管理员
    </s:else>
   </span></td>
    <td><div align="right" class="STYLE1"><a href='deleteUseruserid=<s:property value="#oneUserid"/>' class="STYLE5">删除|</a></div></td>
    <td class="STYLE1"><a href='lookUseruserid=<s:property value="#oneUserid"/>&pageNow=<s:property value="#requestpagepageNow"/>' target="_self" class="STYLE5">修改</a></td>
  </tr>
  </s:iterator>
  
  <tr>
    <td colspan="9"><table width="832" border="0" cellspacing="0" bgcolor="#E7E7E9">
      <s:set name="page" value="#requestpage"></s:set>
    
      <tr>
        <td width="70%">&nbsp;</td>
        <s:if test="#pageisHasPre()">
        <td width="10%"><a href='listUserpageNow=<s:property value="#pagepageNow-1"/>' target="_self" class="STYLE3" >上一页</a></td>
        </s:if>
        <s:else>
         <td width="10%"><a href="listUserpageNow=1" target="_self" class="STYLE3" >上一页</a></td>
        </s:else>
       <s:if test="#pageisHasNext()">
        <td width="10%"><a href="listUserpageNow=<s:property value="#pagepageNow+1"/>" target="_self" class="STYLE3">下一页</a></td>
        </s:if>
       <s:else>
        <td width="10%"><a href="listUserpageNow=<s:property value="#pagetotalPage"/>" target="_self" class="STYLE3">下一页</a></td>
        </s:else>
        <td width="10%"><a href="listUserpageNow=<s:property value="#pagetotalPage"/>" target="_self" class="STYLE3">尾页</a></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

这是采用struts2+hibernate 做的,你可以参考一下

输入&lt;input id="goPage" type="text"&gt;&lt;a href="#" onClick="test()"&gt;

跳转&lt;/a&gt;

这样在输入页码之后点击跳转将要调用test函数

你在test函数中实现跳转就可以了。

如:function test(){//这里还需要实现一些对于输入页码的安全性验证。比如不能为空,必须是数字这些。 var page = documentgetElementById("goPage")value;  windowlocationhref="aajsppage="+page; }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13334746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-16
下一篇 2023-07-16

发表评论

登录后才能评论

评论列表(0条)

保存