首页 > 代码库 > JSP页面分页显示数据

JSP页面分页显示数据

技术分享

效果如上图所示!最多显示10条;
完整jsp和后台代码如下:

 

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="http://www.mamicode.com/${pageContext.request.contextPath}"/>
<html>
     <head>
           <meta charset="UTF-8">
           <title></title>
           <script src="http://www.mamicode.com/${ctx}/js/common/common.js" type="text/javascript"></script>
           <script src="http://www.mamicode.com/${ctx}/js/page/page.js" type="text/javascript"></script>
           <script type="text/javascript">
           
              function firstpage(){
                     $("#pageIndex").val(${page.startPage});
                     $("#form1").submit();
                }
                function lastpage(){
                     $("#pageIndex").val(${page.currPage-1});
                     $("#form1").submit();
                }
                function currpage(){
                     $("#pageIndex").val(${page.currPage});
                     $("#form1").submit();
                }
                function gotoPage(i){
                     var pageNo=$("#gotoPage"+i+"").text();
                     $("#pageIndex").val(pageNo);
                     $("#form1").submit();
                }
                function nextpage(){
                     $("#pageIndex").val(${page.currPage+1});
                     $("#form1").submit();
                }
                function endpage(){
                     $("#pageIndex").val(${page.totalPage});
                     $("#form1").submit();
                }
           
//多文本省略号显示
$(function(){
     $(".company").each(function(){
     var maxwidth=15;
     if($(this).text().length>maxwidth){
     $(this).text($(this).text().substring(0,maxwidth));
     $(this).html($(this).html()+‘…‘);
     }
     });
     })
</script>
     </head>
     <body class="bglightgray"> 
           <div class="div-wrapper" >
                <div class="div-wrapper-con" >
                     <form action="#" method="GET" id="form1" class="fl">
                     <input type="hidden" name="currPage" id="pageIndex" value="http://www.mamicode.com/1" />
                     <%-- <input type="hidden" name="page" id="page" value="http://www.mamicode.com/${userCPs.number+1}"/> --%>
                     <input type="hidden" name="type" id="type" value="http://www.mamicode.com/${type}"/>
                     <input type="hidden" name="m" id="m" value="http://www.mamicode.com/${m}"/>
                <input type="hidden" name="sortType" id="sortType" value="http://www.mamicode.com/${sortType}"/>
                <input type="hidden" name="is_search" value="http://www.mamicode.com/false" id="is_search"/>
                           <div class="tbar clearfix">
                                <span class="fl">请输入cp名称</span>
                                <input name="company" onkeyup="this.value=http://www.mamicode.com/this.value.replace(/^/s+|/s+$/g,‘‘)" value="http://www.mamicode.com/${company}" ty                                pe="text" class="fl inptext" />
                                <span class="fl">请输入排行个数</span>
                                <input id="size" name="page.size" min="1" max="19" type="number" class="fl inptext" />
                                <button  type="submit" id="sub_control" class="fl inpbtns">查询</button>
                           </div>
                     </form>
                     <table class="table"  width="100%">
                           <thead>
                                <tr>
                                     <th width="10%">名次序号</th>
                                     <th width="10%">cp名称</th>
                                     <c:if test="${type==1 }">
                                     <th width="10%">总点击量</th>
                                     </c:if>    
                                     <c:if test="${type==2 }">
                                     <th width="10%">总付费量(/元)</th>
                                     </c:if>    
                                     <c:if test="${type==3}">
                                     <th width="10%">总收藏量</th>
                                     </c:if>                                   
                                </tr>
                           </thead>
                           <tbody>
                                <c:forEach items="${allUser}" var="userCP" varStatus="status">                                
                                                     <tr>
                                                          <td>${(currPage-1)*20+status.index+1}</td>
                                                           <td class="company" title="${userCP[1]}">${userCP[1]}</td>                                                                                                
                                                           <c:if test="${type==1||type==3 }">
                                                          <td>${userCP[2]}</td>
                                                          </c:if>
                                                           <c:if test="${type==2 }">
                                                          <td><fmt:formatNumber type="number" value="http://www.mamicode.com/${userCP[2]}" pattern="#0.00" m                                                          axFractionDigits="2" /></td>
                                                          </c:if>
                                                     </tr>
                                                </c:forEach>
                           </tbody>
                     </table>
                     <div class="pagesdiv clearfix" >
                           <div id="page" class="pagination">
                       <ul>
                            <c:choose>
                                        <c:when test="${page.currPage>1}">
                                           <li><a href="javascript:firstpage()"><<</a></li>
                                           </c:when>
                                           <c:otherwise> <li><a href="http://www.mamicode.com/#"><<</a></li>
                                        </c:otherwise>
                                   </c:choose>
                                   <c:choose>
                                           <c:when test="${page.currPage>1}">
                                           <li> <a href="javascript:lastpage()"><</a> </li>
                                           </c:when>
                                           <c:otherwise><li><a href="http://www.mamicode.com/#"><</a></li>
                                        </c:otherwise>
                                     </c:choose>
                          
                                 <%--计算begin和end --%> 
                                   <c:choose> 
                                       <%--如果总页数不足10,那么就把所有的页都显示出来 --%> 
                                       <c:when test="${requestScope.countPage<=10}"> 
                                           <c:set var="begin" value="http://www.mamicode.com/1" /> 
                                           <c:set var="end" value="http://www.mamicode.com/${requestScope.countPage}" /> 
                                       </c:when> 
                                       <c:otherwise> 
                                           <%--如果总页数大于10,通过公式计算出begin和end --%> 
                                           <c:set var="begin" value="http://www.mamicode.com/${requestScope.currentPage-5}" /> 
                                           <c:set var="end" value="http://www.mamicode.com/${requestScope.currentPage+4}" /> 
                                           <%--头溢出 --%> 
                                           <c:if test="${begin<1}"> 
                                               <c:set var="begin" value="http://www.mamicode.com/1"></c:set> 
                                               <c:set var="end" value="http://www.mamicode.com/10"></c:set> 
                                           </c:if> 
                                           <%--尾溢出 --%> 
                                           <c:if test="${end>requestScope.countPage}"> 
                                               <c:set var="begin" value="http://www.mamicode.com/${requestScope.countPage - 9}"></c:set> 
                                               <c:set var="end" value="http://www.mamicode.com/${requestScope.countPage}"></c:set> 
                                           </c:if> 
                                       </c:otherwise> 
                                   </c:choose> 
                                   <%--循环显示页码列表 --%> 
                            <c:forEach var="i" begin="${begin}" end="${end}">
                                       <c:choose>
                                           <c:when test="${i == page.currPage}">
                                               <li class="active"><a id="current" href="javascript:currpage()" >${i}</a></li>
                                           </c:when>
                                           <c:otherwise>
                                              <li><a href="javascript:gotoPage(${i})" id="gotoPage${i}">${i}</a></li>
                                           </c:otherwise>
                                       </c:choose>
                                   </c:forEach>
                                    <c:choose>
                                        <c:when test="${page.currPage + 1<=page.totalPage }">
                                           <li><a href="javascript:nextpage()">></a></li>
                                           </c:when>
                                           <c:otherwise> <li><a href="http://www.mamicode.com/#">></a></li>
                                        </c:otherwise>
                                   </c:choose>
                                   <c:choose>
                                           <c:when test="${page.currPage + 1<=page.totalPage }">
                                           <li> <a href="javascript:endpage()">>></a> </li>
                                           </c:when>
                                           <c:otherwise><li><a href="http://www.mamicode.com/#">>></a></li>
                                        </c:otherwise>
                                     </c:choose>
                                </ul>
                           </div
                     </div>
                </div>
           </div>
           
           <!-- <script type="text/javascript" src="http://www.mamicode.com/js/jquery-1.11.3.min.js" ></script> -->
           <script>
                $(function(){
                     $("#sub_control").click(function(){
                           $("#is_search").attr("value",true);
                           var data = http://www.mamicode.com/$("#is_search").val();
                           $("#form_sub").click();
                     });
                     $(".table tbody tr").each(function(i){
                           if(i%2==0)
                           {
                                $(this).addClass("bg-white");
                           }else{
                                $(this).addClass("bg-grey");
                           }
                     });
                })
           </script>
     </body>
</html>

  

//java代码分页工具类;
package cn.enetic.wodm.entity;

import java.util.HashMap;
import java.util.Map;
/**
 * 分页
 * @author Administrator
 *
 */
public class PageUtil {
    private int totalNum;// 总记录数;
    private int totalPage;// 总页数
    private int currPage;// 当前页码;
    private int pageNum = 20;// 默认页面记录数;
    private int startNum = 1;// 开始数;
    private int endNum;// 结束数目;
    private int startPage;// 开始页;
    private int endPage;// 结束页;

    public PageUtil() {

    }
    public PageUtil(int pageNum,String key,Object value) {
        params.put(key, value);
        this.pageNum = pageNum;
    }
    public PageUtil(int pageNum){
        this.pageNum = pageNum;
    }
    private Map<String, Object> params=new HashMap<String, Object>();// 鍙傛暟鍒楄??

    public void setTotalPage(int totalNum) {
        int num = totalNum % pageNum;
        if (num == 0) {
            totalPage = totalNum / pageNum;
        } else {
            totalPage = totalNum / pageNum + 1;
        }
    }

    public void initPage(int totalNum) {
        this.totalNum = totalNum;
        setTotalPage(totalNum);// 璁剧疆鎬婚??闈㈡??
        startPage = 1;
        endPage = totalPage;
        if(currPage<=0){
            this.currPage=1;
        }else if(currPage>=totalPage){
            this.currPage=totalPage;
        }
        startNum = pageNum * currPage - pageNum ;
        if(startNum<=0){startNum=0;}
        endNum = pageNum * currPage;
        if (endNum > totalNum) {
            endNum = totalNum;
        }

    }

    public int getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(int totalNum) {
        this.totalNum = totalNum;
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int pageNumber) {
        if("".equals(pageNumber)){
            this.currPage=1;
        }else{
            this.currPage=pageNumber;
        }
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getStartNum() {
        return startNum;
    }

    public void setStartNum(int startNum) {
        this.startNum = startNum;
    }

    public int getEndNum() {
        return endNum;
    }

    public void setEndNum(int endNum) {
        this.endNum = endNum;
    }

    public int getStartPage() {
        return startPage;
    }

    public void setStartPage(int startPage) {
        this.startPage = startPage;
    }

    public int getEndPage() {
        return endPage;
    }

    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public int getTotalPage() {
        return totalPage;
    }
}

  

//control层代码; //如果点击了查询按钮,页数清空
// 分页总数;
 int totalnumber=0;
     if(("".equals(company)||company==null)&&pageSize!=20){                
            totalnumber=pageSize;
            currPage = 1;
           }else{
            totalnumber = this.userCPService.getTotalnumber(company);
           }
           
           PageUtil page = new PageUtil(20);
           page.setCurrPage(currPage);
           page.initPage(totalnumber);
           int startNum = page.getStartNum();
           // 获取审核通过的usercp和对应的点击浏览数;
           List<Object[]> allUser = this.userCPService.getAllPassUserCPsAndClicks(startNum,pageSize, company);
           
           model.addAttribute("allUser", allUser);
           model.addAttribute("page", page);
           model.addAttribute("company", company);
           model.addAttribute("type", type);
           model.addAttribute("sortType", sortType);
           model.addAttribute("PAGE_SIZE", "20");
           model.addAttribute("currPage", currPage);
           model.addAttribute("navigateColor", navigateColor);
           model.addAttribute("m", m);
                return "RankStatistics/cpRankList";    
     }

  


  




JSP页面分页显示数据