SpringMVC 分页

  1. JSP页面
<div align="right">
        <p><span id="sShowTotalNum">${busSite.totalNum }</span>条,
            第<span id="sShowPageNumber">${busSite.pageNumber }</span>/
            <span id="sShowTotalPage">${busSite.totalPage }</span>页,
            <a href="javascript:goPageAction(1);">首页</a>
            <a href="javascript:goUpPage();" >上一页</a>
            <a href="javascript:goDownPage();" >下一页</a>
            <a href="javascript:goEndPage();">末页</a>
            到第<input id="inpGoPage" size="10%"/><input type="button" value="GO" id="btnGoPage"/>
        </p>
    </div>

2.jS

$(function(){

      /* 点击去第几页 */
       $("#btnGoPage").click(function(){
          var value = $("#inpGoPage").val();
          var js = /^\d+$/;
          if(value.match(js)){
              var totalPage = $("#sShowTotalPage").text();
              if(value>parseInt(totalPage) || value<1){
                  showModal("myModal","输入不合法!");
              }else{
                  goPageAction(value);
              }
          }else{
              showModal("myModal","输入不合法!");
          }
      })
})

/* 上一页 */
function goUpPage(){
    var pageNum = parseInt($("#sShowPageNumber").text()) -1;
    if(pageNum<=0){
        return;
    }else{
        goPageAction(pageNum);
    }
}

/* 下一页 */
function goDownPage(){
    var pageNum = parseInt($("#sShowPageNumber").text()) +1;
    var totalPage = parseInt($("#sShowTotalPage").text());
    if(pageNum>totalPage){
        return;
    }else{
        goPageAction(pageNum);
    }
}

/* 末页 */
function goEndPage(){
    var pageNum = parseInt($("#sShowTotalPage").text());
        goPageAction(pageNum);
}

/* 查询符合条件的站点 */
function goPageAction(pageNum){
     $.ajax({
          type: 'POST',
          url: '/CityInfoCloud_GJ_WEB/1_0/GJ/szgj/051201&051200/getSearchedSiteList/0',
          data: {"pageNumber":pageNum,"cityCode":selCityCodeVal,"busSiteName":txtSiteNameVal},
          success: function(data){
              $("#tbResult tbody").remove();
              if(data.length>0){
                  var pageSize = data[0].pageSize;
                  var pageNumber = data[0].pageNumber;
                  $("#sShowTotalNum").text(data[0].totalNum);
                  $("#sShowPageNumber").text(pageNumber);
                  $("#sShowTotalPage").text(data[0].totalPage);
                  var prevSort = eval(pageSize * (pageNumber -1));

                  for(var i=1;i<data.length;i++){
                        var $tr = "<tr><td>"+(i +prevSort)+"</td><td>"
                        +data[i].cityName+"</td><td>"
                        +data[i].busSiteCode+"</td><td>"
                        +data[i].busSiteName+"</td><td>"
                        +data[i].busSiteAlias+"</td><td>"
                        +data[i].formatDate+"</td><td><a href='/CityInfoCloud_GJ_WEB/1_0/GJ/szgj/051201&051200/getSiteDetailPage/0?siteId="+data[i].busSiteID+"'>详情</a>&nbsp;&nbsp;&nbsp;"
                        +"<a href='javascript:deleteSite(&quot;"+data[i].busSiteID+"&quot;)'>删除</a></td></tr>";
                        $("#tbResult").append($tr);
                    }

              }

          }
        });
    }

3.Controller(经分发后)

@RequestMapping("getSearchedSiteList")
    public void getSearchedSiteList(HttpServletRequest request, HttpServletResponse response,BusSite busSite) throws BaseException {
        String appId = (String)request.getAttribute("appId");
        String areaCode = (String)request.getAttribute("areaCode");
        busSite.setAppID(appId);
        List<BusSite> siteList = busSiteManageServiceImpl.findByCityAndSite(busSite, areaCode);
        String json = JSONArray.toJSONString(siteList);
        PrintWriter out = null;
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");

        try {
            out = response.getWriter();
            out.write(json);
        } catch (IOException e) {
            throw new BaseException(e);
        }
    }

4.ServicerImpl

@Override
    public List<BusSite> findByCityAndSite(BusSite busSite, String areaCode) throws BaseException {
        String[] cityCodes = new String[1];
        String cityCode = busSite.getCityCode();
        if(cityCode.length()==0){
            cityCodes = areaCode.split("&");
        }else{
            cityCodes[0] = cityCode;
        }

        List<BusSite> siteList = new ArrayList<BusSite>();
        siteList.add(busSite);
//      查询总个数
        int total=0;
        total=(this.busSiteDAO.searchPagingTotalWithCond( cityCodes, busSite));
        busSite.setTotalNum(total);
        if(total==0){
            total=1;
        }
        busSite.initPaging((long)total);
//      分页查询
        siteList.addAll(busSiteDAO.findByCityAndSite(busSite, cityCodes));
        return siteList;
    }

5.对于被分页的Bean,有属性与方法:

public class BusSite<T>{
.....

    private int pageNumber;//第几页
    private int pageSize;//每页个数
    private long totalPage;//总页数
    private long totalNum;//总条数

    public BusSite(){//初始化
        this.pageNumber = 1;
        this.pageSize =10;
    }
    //计算总页数
    public T initPaging(long totalCount){
            long val = totalCount%pageSize;
            if(val==0){
                this.totalPage = totalCount/pageSize;
            }else{
                this.totalPage = totalCount/pageSize +1;
            }

            if (this.pageNumber==-1||this.pageNumber>this.totalPage) {
                this.pageNumber = (int) this.totalPage;
            }

            return (T) this;
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值