日期:2014-05-18  浏览次数:20695 次

怎样实现分页显示
如题。比如论坛相关话题帖子多了
要分成多页。那什么方式是最简便

------解决方案--------------------
写个分页的BEAN。
至于采用什么方式分,看你自己了,可以用SQL相减,也可以用SQL取出全部的值,再用几个变量运算
------解决方案--------------------
呵呵,要是用hibernate的话,分页就很简单了!
------解决方案--------------------
下面是我做blog的时候用到的分页 你要不觉得恶心的话就用吧 我也是初学者 哪里写错了还请指出来哈 
<%@tag pageEncoding="gb2312" body-content="empty"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@attribute name="diarysortid" type="java.lang.Integer" required="true"%>
<%@attribute name="page" type="java.lang.Integer" required="true"%>
<%@attribute name="rowseverypage" type="java.lang.Integer"
required="true"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<sql:setDataSource var="conn" driver="sun.jdbc.odbc.JdbcOdbcDriver"
url="jdbc:odbc:JBlog" />


<c:choose>
<c:when test="${diarysortid==0}">
<sql:query var="row" dataSource="${conn}">
select count(*) from articles
</sql:query>

<c:choose>
<c:when test="${row.rowsByIndex[0][0]%rowseverypage==0}">
<c:set var="rows" value="${row.rowsByIndex[0][0]/rowseverypage}"></c:set>
</c:when>

<c:otherwise>
<c:set var="rows" value="${row.rowsByIndex[0][0]/rowseverypage+1}"></c:set>
</c:otherwise>
</c:choose>

<sql:query var="rs" maxRows="${rowseverypage}"
startRow="${(page-1)*rowseverypage}" dataSource="${conn}">
select diarysortname=(select top 1 diarysortname from diarysort where diarysortid=a.diarysortid),diarysortid,articletitle,articleid=articleid,author=(select top 1 username from userinfo where userid=articleauthorid),articleauthorid,[date],commentcounts,readcounts from articles as a order by [date] desc
</sql:query>
</c:when>

<c:otherwise>
<sql:query var="row" dataSource="${conn}">
select count(*) from articles where diarysortid=${diarysortid}
</sql:query>

<c:choose>
<c:when test="${row.rowsByIndex[0][0]%rowseverypage==0}">
<c:set var="rows" value="${row.rowsByIndex[0][0]/rowseverypage}"></c:set>
</c:when>

<c:otherwise>
<c:set var="rows" value="${row.rowsByIndex[0][0]/rowseverypage+1}"></c:set>
</c:otherwise>
</c:choose>

<sql:query var="rs" maxRows="${rowseverypage}"
startRow="${(page-1)*rowseverypage}" dataSource="${conn}">
select diarysortname=(select top 1 diarysortname from diarysort where diarysortid=a.diarysortid),diarysortid,articletitle,articleid=articleid,author=(select top 1 username from userinfo where userid=articleauthorid),articleauthorid,[date],commentcounts,readcounts from articles as a where diarysortid=${diarysortid} order by [date] desc 
</sql:query>
</c:otherwise>
</c:choose>

<c:if test="${!(empty rs)}">
<p>
<h2>${page}</h2>
<hr>
<table width="100%" cellpadding="1">
<c:forEach items="${rs.rows}&