怎样实现分页显示
如题。比如论坛相关话题帖子多了
要分成多页。那什么方式是最简便
[解决办法]
写个分页的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}" var="article">
<tr>
<td>
<a href="index.jsp?diarysortid=${article.diarysortid}">[${article.diarysortname}]</a>
</td>
<td width="500">
<a href="index.jsp?articleid=${article.articleid }">${article.articletitle }</a>
</td>
<td>
<a href="index.jsp?user=${article.articleauthorid }>">${article.author}</a>
</td>
<td>
<font style="color:#979797">${fn:substringBefore(article.date," ")}</font>
</td>
<td>
<font style="color:#0066FF">${article.commentcounts }</font> | ${article.readcounts }</td>
</tr>
</c:forEach>
</table>
<hr>
<c:forEach var="i" begin="1" end="${rows}">
<c:choose>
<c:when test="${!(diarysortid==0)}">
<c:choose>
<c:when test="${i==page}">
<c:out value="${i}"></c:out>
</c:when>
<c:otherwise>
<a href="index.jsp?page=${i}&diarysortid=${diarysortid }">${i}</a>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${i==page}">
<c:out value="${i}"></c:out>
</c:when>
<c:otherwise>
<a href="index.jsp?page=${i}">${i}</a>
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
</c:forEach>
</c:if>