第一次写分页的代码

因为一个机器连接到和它相关的机器上的时候,当和它连接的机器很多的时候出现了速度很慢的情况,于是要我写了一个分页的机能。页面不适合用displayTag,所以, 要求 手动的分页程序。

首先是Action部分:

1。//一个页面出现多条数据,就一定会出现分页的情况了,当点击页数5时候,进去5,有情况需要返回,所以5需要保存,  PageNo 用来保存当前的页数。以下的代码是,从url中读取,页数,如果没有,默认为1

        String pageNo = request.getParameter("pageNo");
        if (pageNo == null || pageNo.equals("")) {
            pageNo = new String("1");
        }
        int thisPageNoInt = Integer.parseInt(pageNo);

2。// 然后需要知道,你显示数据的总共数量--lanCount ,总共可以显示的页数--pageCount, pageSize是当前页面单页显示的数量。

        String pageCount = "";     
        Integer lanCount = mgr.getConnectInfoCount(assetMap);

        int lanCountInt = 0;
        if (lanCount != null) {
            lanCountInt = lanCount.intValue();
        }
        int pageSize = Constants.KIKI_PAGE_SIZE;
        int pageCountInt = 1;
        if (lanCountInt % pageSize != 0) {
            pageCountInt = lanCountInt / pageSize + 1;
        } else {
            pageCountInt = lanCountInt / pageSize;
        }
        // the all pageCount
        pageCount = String.valueOf(pageCountInt);
        String pageCount1 = String.valueOf(pageCountInt - 1);

3。 // 然后是,取得显示数据的list,...具体问题具体分析。。。呵呵。但需要传入pageNo,pageSize

        List rsList = new ArrayList();
        Map astMap = new HashMap();       
        astMap.put("pageNo", Integer.parseInt(pageNo));
        astMap.put("pageSize", new Integer(pageSize));
        astMap.put("astId", Integer.valueOf(astId));
        wcompanyId = Integer
                .valueOf(getOptionInfo(Constants.OPATION_WCOMPANY_ID));
        astMap.put("wcompanyId", wcompanyId);
        rsList = mgr.getConnectInfo(astMap);

4。// 然后, max 是当前页显示的数据量, skip是前面跳过的数据量,  recodeFrom是当前页的开始位置, recodeTo是当前页的截止位置, 然后把相应的数据传给下个页面。这里的数据主要是给jsp页面使用的。。。

        int max = pageSize;
        int skip = (thisPageNoInt - 1) * max;
        if (lanCountInt - skip < max) {
            max = lanCountInt - skip;
        }

        String recodeFrom = String.valueOf(skip + 1);
        String recodeTo = String.valueOf(skip + max);

        request.setAttribute("recodeCount", lanCount);
        request.setAttribute("recodeFrom", recodeFrom);
        request.setAttribute("recodeTo", recodeTo);
        request.setAttribute("pageCount", pageCount);
        request.setAttribute("pageCount1", pageCount1);
        request.setAttribute("page1", String.valueOf(thisPageNoInt - 1));
        request.setAttribute("page3", String.valueOf(thisPageNoInt + 1));
        if ((thisPageNoInt + 2) < pageCountInt) {
            request.setAttribute("page4", String.valueOf(thisPageNoInt + 2));
        }
        request.setAttribute("thisPageNo", String.valueOf(thisPageNoInt));
        request.setAttribute("formList", rsList);

5。 然后service 部分的代码是正常写, 3。部分已经说明了。需要传入pageNo和pageSize,然后再daoIbatis里面通过传入的二个参数,进行和上面4。部分,相似的计算,得到skip和max。就知道显示得数据了。sql文部分,不用更改。

        int pageNo = (Integer) astMap.get("pageNo");
        int max = (Integer) astMap.get("pageSize");
        int skip = (pageNo - 1) * max;
        int astId = (Integer) astMap.get("astId");
        // getComputerCount
        Integer records = (Integer) getSqlMapClientTemplate()
                .queryForObject("LanCableInfo.getSimpleLanCableInfoCount",
                        astId);
        // TODO get count
        int recordsize = records.intValue();
        if (recordsize - skip < max) {
            max = recordsize - skip;
        }       
        List lanCableList = getSqlMapClientTemplate().queryForList(
                "LanCableInfo.getSimpleLanCableInfo", astId, skip, max);

 

jsp部分的代码如下:

<!-- follow me is the data page show add by yjy at 2007/03/08 -->
        <tr>              
                     <td align="left">
                      &nbsp;&nbsp;&nbsp;&nbsp;
                      <c:out value="${recodeFrom}"/>/<c:out value="${recodeTo}"/>
                      &nbsp;&nbsp;
                      (<fmt:message key='ITIL310F.total'/>&nbsp;<c:out value="${recodeCount}"/>)
               <input type="hidden" name="pageCount" value="<c:out value='${pageCount}'/>">
               <input type="hidden" name="thisPageNo" value="<c:out value='${thisPageNo}'/>">
                   <c:if test="${pageCount != 1}">
                    [<c:if test="${thisPageNo != 1}">
                     <a οnclick="first()" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><fmt:message key='ITIL310F.first'/></a>
                     <a οnclick="pre()" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><fmt:message key='ITIL310F.pre'/></a>
                    </c:if>
                    <c:if test="${thisPageNo == 1}">
                     <a style="font-size: .95em"><fmt:message key='ITIL310F.first'/></a>
                     <a style="font-size: .95em"><fmt:message key='ITIL310F.pre'/></a>
                    </c:if>]

        <c:if test="${thisPageNo != 1}">
                     <a οnclick="goTo(<c:out value="${page1}"/>)" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><c:out value="${page1}"/></a>,
                    </c:if>
                    <span style="font-size: .95em"><STRONG><c:out value="${thisPageNo}"/></STRONG></span>
                    

                    <c:if test="${thisPageNo != pageCount1 && thisPageNo != pageCount}">
                     ,<a οnclick="goTo(<c:out value="${page3}"/>)" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><c:out value="${page3}"/></a>
                    </c:if>
                    <c:if test="${thisPageNo == pageCount1}">
                     ,<a οnclick="goTo(<c:out value="${page3}"/>)" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><c:out value="${page3}"/></a>
                    </c:if>                    
                    
                    <c:if test="${(thisPageNo != pageCount) && ( not empty page4)}">
                     ,<a οnclick="goTo(<c:out value="${page4}"/>)" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><c:out value="${page4}"/></a>
                    </c:if>
                    
                    <c:if test="${(thisPageNo == 1) && ( pageCount > 4)}">
                     ,<a οnclick="goTo(<c:out value="${4}"/>)" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em">4</a>
                    </c:if>       
                    [<c:if test="${thisPageNo != pageCount}">
                     <a οnclick="next()" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><fmt:message key='ITIL310F.next'/></a>
                     <a οnclick="last()" style="cursor:hand;text-decoration:underline;color:blue;font-size: .95em"><fmt:message key='ITIL310F.last'/></a>
                    </c:if>
                    <c:if test="${thisPageNo == pageCount}">
                     <a style="font-size: .95em"><fmt:message key='ITIL310F.next'/></a>
                     <a style="font-size: .95em"><fmt:message key='ITIL310F.last'/></a>
                    </c:if>]&nbsp;&nbsp;&nbsp;&nbsp;
        <fmt:message key='ITIL310F.total'/>&nbsp;<c:out value="${pageCount}" />
                   </c:if>
                        </td>
        </tr>     
<!-- follow me is the data page show add by yjy at 2007/03/08 -->     

 下面是对应jsp的onclick方法。脚本

function first() {
 ITIL211Form.action="ITIL211.html?pageNo=1&method=connectConfirm&fromMethod=gotoAnyPage";
 ITIL211Form.οnsubmit="";
 ITIL211Form.submit();

function last() {
 var last =  parseInt(ITIL211Form.pageCount.value);
 ITIL211Form.action="ITIL211.html?pageNo=" + last + "&method=connectConfirm&fromMethod=gotoAnyPage";
 ITIL211Form.οnsubmit="";
 ITIL211Form.submit();

function pre() {
 var pre =  parseInt(ITIL211Form.thisPageNo.value) - 1;
 ITIL211Form.action="ITIL211.html?pageNo=" + pre + "&method=connectConfirm&fromMethod=gotoAnyPage";
 ITIL211Form.οnsubmit="";
 ITIL211Form.submit();
}
function next() {
 var next = parseInt(ITIL211Form.thisPageNo.value) + 1;
 ITIL211Form.action="ITIL211.html?pageNo=" + next + "&method=connectConfirm&fromMethod=gotoAnyPage";
 ITIL211Form.οnsubmit="";
 ITIL211Form.submit();
}
function goTo(pageNum) {
 var pageNo = pageNum;
 var pageNoNum = parseInt(pageNo);
 ITIL211Form.action="ITIL211.html?pageNo=" + pageNoNum + "&method=connectConfirm&fromMethod=gotoAnyPage";
 ITIL211Form.οnsubmit="";
 ITIL211Form.submit();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值