JSTL分页

  
    
< c:set var ="basePath" >${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/ </ c:set >
< base href= "${basePath}" >
< c:set var ="columns" value ="5" ></ c:set >
< table cellspacing ="0" cellpadding ="0"   >
< tr >
< th style ="text-align:center" colspan ="${columns }" >< h3 > 列表 </ h3 ></ th >
</ tr >
< c:set var ="page" value ="${page}" ></ c:set >
< c:forEach var ="word" varStatus ="status" items ="${page.result}" >
< c:if test ="${status.index%columns == 0}" >< tr ></ c:if >
< td >< div > ${word[0]} </ div >< div class ="colon" > : </ div >< div class ="view" > ${word[1] } </ div ></ td >
< c:if test ="${status.last&&status.index%columns != (columns-1)}" >
< c:forEach begin ="1" end ="${columns-1 - status.index%columns}" step ="1" >
< td ></ td >
</ c:forEach >
</ c:if >
< c:if test ="${status.index%columns == columns-1||status.last}" ></ tr ></ c:if >
</ c:forEach >
< c:set var ="btns" value ="5" ></ c:set >
< c:set var ="halfbtns" value ="${btns%2==0?btns/2:(btns-btns%2)/2}" ></ c:set >
< c:set var ="middlebtns" value ="${halfbtns + 1}" ></ c:set >
< c:set var ="pgNum" value ="${page.pageNo}" ></ c:set >
< c:set var ="pgSize" value ="${page.pageSize}" ></ c:set >
< c:set var ="totalCount" value ="${page.totalCount}" ></ c:set >
< c:set var ="totalPage" value ="${page.totalPage}" ></ c:set >
< c:if test ="${totalCount - pgSize>0}" >
< tr >
< td colspan ="${columns }" >
< c:forEach begin ="${pgNum<middlebtns?1:((pgNum+halfbtns-totalPage)>0?((totalPage-btns + 1 > 0)?(totalPage-btns + 1):1):(pgNum-halfbtns))}" end ="${pgNum<middlebtns?((totalPage>btns)?btns:totalPage):(pgNum>(totalPage-halfbtns)?totalPage:(pgNum+halfbtns))}" varStatus ="stat" >
< c:if test ="${pgNum>1&&stat.first}" >< a class ="pnBtn" href ="${basePath}servlet?pageno=1&pagesize=${pgSize}" > 首页 </ a >< a class ="pnBtn" href ="${basePath}servlet?pageno=${pgNum-1}&pagesize=${pgSize}" > 上一页 </ a >< c:if test ="${stat.index>1}" > ... </ c:if ></ c:if >
< c:if test ="${stat.index<=totalPage}" >
< c:choose >
< c:when test ="${stat.index == pgNum}" >
< span class ="current" > ${stat.index } </ span >
</ c:when >
< c:otherwise >
< a   href ="${basePat}hservlet?pageno=${stat.index}&pagesize=${pgSize}" > ${stat.index } </ a >
</ c:otherwise >
</ c:choose >
</ c:if >
< c:if test ="${(pgNum-totalPage<0)&&stat.last}" >< c:if test ="${stat.index<totalPage}" > ... </ c:if >< a class ="pnBtn" href ="${basePath}servlet?pageno=${pgNum+1}&pagesize=${pgSize}" > 下一页 </ a >< a class ="pnBtn" href ="${basePath}servlet?pageno=${totalPage}&pagesize=${pgSize}" > 末页 </ a ></ c:if >
</ c:forEach >
</ td >
</ tr >
</ c:if >
</ table >
其实这begin和end写的逻辑我自己都有些迷糊了...

以显示5个按钮为例,

对于起始页,即显示的第一个页码按钮

1.如果当前页小于3,起始页固定为1,

2.如果当前页大于3(包括3),

  1.当前页与尾页的差少于2,

    1.如果总页数在5页以上(包括5),起始页固定为尾页数过来第5个

    2.总页数小于5,起始页固定为1

  2.正常情况,起始页为当前页-2

 

对于末页,即显示的最后一个页码按钮

1.如果当前页小于3

  1.如果总页数比5要大,末页为5

  2.比5小,末页为尾页

2.当前页大于3

  1.当前页与尾页差小于2,末页为尾页

  2.否则末页为当前页+2


上面例子中page.result返回的是一个List<Object[]>集合

你可能感兴趣的:(jstl)