posts - 10, comments - 9, trackbacks - 0, articles - 17

在MVC中实现的分页。

Posted on 2008-11-27 13:34 wesley1987 阅读(750) 评论(1)  编辑  收藏 所属分类: struts学习项目

1.。JSP页面中:

////////////数据显示///////////////////////
显示 从数据库查出的,放在 list<offer> 中的数据。

<c:forEach items="${requestScope.list}" var="offer">
    
<tr>
      
<td height="25">${offer.offername}</td>
      
<td height="25">${offer.offeradd}</td>
      
<td height="25">${offer.tel}</td>
      
<td height="25">${offer.connecter}</td>
      
<td height="25">${offer.bank}</td>
      
<td height="25">${offer.account}</td>
      
<td height="25">${offer.post}</td>
      
<td height="25" onclick="showComment(${offer.offerId})"><p id="getshow${offer.offerId}"> 查看备注 </p><p id=${offer.offerId} style="display:none">${offer.comment}</p></td>
      
<td height="25"><a href="">修改</a></td>
      
<td height="25"><a onclick="deleteCheck('${offer.offername}')">删除</a></td>
       
</tr></c:forEach>
      
<script>
        function showComment(sid)
{
                     
if(document.getElementById(sid).style.display=="none"){
                             document.getElementById(sid).style.display
="inline";
                             document.getElementById(
"getshow"+sid).style.display="none";}
 
                     
else {
                             document.getElementById(sid).style.display
="none";
                             document.getElementById(
"getshow"+sid).style.display="inline";}


            }


          function deleteCheck(offername)
{
                         var name
=offername;
                   
if(confirm("确认删除供应商 :"+name+" ?"))
                            window.location
="<%=path%>/service/message.jsp?message=needtochange";
                           
return;
          }
</script>

////////
///////////分页选择单    //////////////////////
参数说明    每次显示 5个页面连接,按 '<'  '>'  则显示上/下5个页面:
 pagePart 以5页面为以part。从1开始
 currPage 当前页数
pageCount 总页数

 1<div class=digg align="right">
 2     <c:choose>
 3              <c:when test="${requestScope.pagePart>1}">
 4                      <a href="<%=path%>/offerSearch.do?offername=${requestScope.offername}&page=${requestScope.pagePart*5-5}">&lt; </a>&nbsp;&nbsp;  
 5              </c:when>
 6               <c:otherwise>
 7                           <span class=disabled>&lt;</span>
 8               </c:otherwise>
 9          </c:choose>
10
11  <c:forEach begin="${(requestScope.pagePart*5-4)>0?requestScope.pagePart*5-4:1}" end="${requestScope.pagePart*5<requestScope.pageCount?requestScope.pagePart*5:requestScope.pageCount}" var="page">
12        <c:choose>
13             <c:when test="${requestScope.currPage==page}"><span class=current>${page}</span></c:when>
14            <c:otherwise><a href="<%=path%>/offerSearch.do?offername=${requestScope.offername}&page=${page}">${page}</a></c:otherwise>
15        </c:choose>
16  </c:forEach>
17  
18  <c:choose>
19          <c:when  test="${requestScope.pagePart*5<requestScope.pageCount}">
20 &nbsp;&nbsp;<a href="<%=path%>/offerSearch.do?offername=${requestScope.offername}&page=${requestScope.pagePart*5+1}"">&gt; </a>
21          </c:when>
22          <c:otherwise><span class=disabled>&gt;</span></c:otherwise>
23  </c:choose><span class=disabled>${requestScope.currPage}/${requestScope.pageCount}</span>
24</div>
25
26
分页处用到的CSS
DIV.digg {
 PADDING-RIGHT
: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center
}

DIV.digg A 
{
 BORDER-RIGHT
: #aaaadd 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #aaaadd 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #aaaadd 1px solid; COLOR: #000099; PADDING-TOP: 2px; BORDER-BOTTOM: #aaaadd 1px solid; TEXT-DECORATION: none
}

DIV.digg A:hover 
{
 BORDER-RIGHT
: #000099 1px solid; BORDER-TOP: #000099 1px solid; BORDER-LEFT: #000099 1px solid; COLOR: #000; BORDER-BOTTOM: #000099 1px solid
}

DIV.digg A:active 
{
 BORDER-RIGHT
: #000099 1px solid; BORDER-TOP: #000099 1px solid; BORDER-LEFT: #000099 1px solid; COLOR: #000; BORDER-BOTTOM: #000099 1px solid
}

DIV.digg SPAN.current 
{
 BORDER-RIGHT
: #000099 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #000099 1px solid; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #000099 1px solid; COLOR: #fff; PADDING-TOP: 2px; BORDER-BOTTOM: #000099 1px solid; BACKGROUND-COLOR: #000099
}

DIV.digg SPAN.disabled 
{
 BORDER-RIGHT
: #eee 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #eee 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #eee 1px solid; COLOR: #ddd; PADDING-TOP: 2px; BORDER-BOTTOM: #eee 1px solid
}


2 处理的Action
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception 
{
        String offername = Model.changeStr(request.getParameter("offername"));
        request.setAttribute("offername", offername);
        request.setAttribute("pageCount", OfferSearchModel.getPageCount(offername));
        
        if(request.getParameter("page")==null){
            request.setAttribute("list", OfferSearchModel.getOfferList(offername, 1));        
            request.setAttribute("pagePart", 1);
            request.setAttribute("currPage", 1);
        
}

        else
{
            int page = Integer.parseInt(request.getParameter("page"));
            int pagePart = (page-1)/5+1;
            request.setAttribute("list", OfferSearchModel.getOfferList(offername, page));
            request.setAttribute("pagePart", pagePart);
            request.setAttribute("currPage", page);
        
}
        
        return mapping.findForward("show");
    }

3 数据库查询 (pageSize 每页显示条数)
String sql="select count(*) from offer where offername like '%"+offername+"%'";

sql = sql+"where offername like '%"+offername+"%' and rownum <="+pageSize
    +"and offerid not in (select offerid from offer where offername like '%"+offername
    +"%' and rownum <="+pageSize*(page-1)+")";            



后记:第三步的SQL语句 本来是根据top-N的方法得来的,但是在Oracle中使用后发现个错误:
比如取第5页数据:
select * from offer where offername like 'offername' and rownum <=10
    
and offerid not in (select offerid from offer where offername like 'offername' and rownum <=10*(5-1));     
错误原因是 ()中的子查询 和外面的查询的排序方式不同!!非常诡异的问题,折腾一下午也没解决。
现在实际用的是全取出后,再截取需要数据。虽然这个方法不好,但是可以方便做成公共的接口和实现。

这个方法的Oracle替换方法 可以考虑用过程函数的游标实现。还有用集合运算中的MINUS相减,算法应该和上面的是一样的。


Feedback

# re: 在MVC中实现的分页。  回复  更多评论   

2013-11-25 11:49 by 扯淡
4223

只有注册用户登录后才能发表评论。


网站导航: