jsp代码如下:
<c:url var="paginationAction" value="spaceadmin.groupTypeManage.do"></c:url>
<c:if test="${pageObject.pageCount>1}">
<div class="pagination">
<c:if test="${pageObject.currentIndex!=1}">
<c:url var="url" value="${paginationAction}">
<c:param name="page" value="${1}"/>
</c:url>
<a href='<c:out value="${url}"/>'>首页</a>
<c:url var="url" value="${paginationAction}">
<c:param name="page" value="${pageObject.previousIndex}"/>
</c:url>
<a href='<c:out value="${url}"/>'>上一页</a>
</c:if>
<c:forEach var="x" begin="${pageObject.startIndexOnShow}" end="${pageObject.endIndexOnShow}" step="1" varStatus="status">
<c:url var="url" value="${paginationAction}">
<c:param name="page" value="${x}"/>
</c:url>
<c:choose>
<c:when test="${pageObject.currentIndex==x}"><strong><c:out value="${x}"/></strong></c:when>
<c:otherwise>
<a href='<c:out value="${url}"/>'><c:out value="${x}"/></a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pageObject.currentIndex<pageObject.endIndex}">
<c:url var="url" value="${paginationAction}">
<c:param name="page" value="${pageObject.nextIndex}"/>
</c:url>
<a href='<c:out value="${url}"/>'>下一页</a>
<c:url var="url" value="${paginationAction}">
<c:param name="page" value="${pageObject.endIndex}"/>
</c:url>
<a href='<c:out value="${url}"/>'>尾页</a>
</c:if>
</div>
</c:if>
pageObject为PagingSupport类型,是后台封装的一个包含数据的对象。
public class PaginationSupport implements Serializable {
private static final long serialVersionUID = 1L;
public final static int PAGESIZE = 30;
private int indexCountOnShow = 7; // 默认最多显示7个页码
private int pageSize = PAGESIZE;
private List items;
private int totalCount;
private int[] indexes = new int[0];
private int startIndex = 0;
private int nextIndex;
private int previousIndex;
private int pageCount;
private int currentIndex;
private int endIndex;
.....
List items 里面保存从数据库中得到的对象。至于分页的sql,因为公司只使用oracle的数据库,所以采用的就是类似如下的sql了:
select * from (select t1.*, rownum rn from (select * from t_photo) t1 where rownum<:num2) where rn>:num1
这样每次从数据库得到的list先要塞到一个PagingSupport对象里,然后返回。
从上面的jsp可以看到,因为pageObject是写死在分页的页面里,所以页面上必须要有pageObject变量,而且只能有一个,这就显得不是很灵活了。