当前位置:首页 > 开发 > 编程语言 > JSP > 正文

java后台与 jsp页面的分页案例

发表于: 2012-07-27   作者:colddemon   来源:转载   浏览:
摘要: java后台的方法 public int page; /** 页数索引* */ public int pageIndex; /** 首页索引* */ public int index; /** 末页* */ public int max; /** * @see 获取新闻列表并分页 * @return */ public String
java后台的方法

public int page;
 /** 页数索引* */
 public int pageIndex;
 /** 首页索引* */
 public int index;
 /** 末页* */
 public int max;


/**
  * @see 获取新闻列表并分页
  * @return
  */
 public String getNewsList() {
  if(this.checkLogin()){
  pageSize = 5;
  if (page < 1) {
   page = 1;
  }
  int startIndex = (page - 1) * 5;
  newslist = newsService.getPage(startIndex, pageSize);
  intRowCount = newsService.getTotalCount();
  k = (intRowCount + pageSize - 1) / pageSize;
  intPageCount = (intRowCount + pageSize - 1) / pageSize;// 计算出总页数
  if (page > intPageCount) {
   page = intPageCount;
  }
  // f = (page -1)*pageSize;
  pageIndex = page;
  if (intPageCount <= 5) {
   index = 1;
   max = intPageCount;
  } else if (page < 5) {
   index = 1;
   max = 5;
  } else if (page / 5 != intPageCount / 5) {
   index = page - 2;
   if (intPageCount >= page + 2) {
    max = page + 2;
   } else {
    max = intPageCount;
   }
  } else {
   index = intPageCount - 4;
   max = intPageCount;
  }

  return SUCCESS;
  }else{
  return LOGIN;
  }
 }



jsp前端:


 
<div class="ddxx_nei">
                        <div class="ddxx_ti"> 
                        <s:url action="addnewsUI" var="addnewsUI"></s:url>
                  <s:a href="%{addnewsUI}" cssClass="ddxx_ti">添加新闻</s:a></div>
                        <div class="ddxx_biaoge">
                          <table width="730" cellspacing="0" cellpadding="0" class="shangpin">
                            <tr class="se">
                              <td width="143">编号</td>
                              <td width="303">新闻标题</td>
                              <td width="140">创建日期</td>
                              <td width="142">操作</td>
                            </tr>
                          <s:iterator value="newslist">
                  <tr>
                  <td><s:property value="newid"/>
              </td>
              <td><s:url action="displaynews" var="displaynews">
              <s:param name="id">
            <s:property value="newid"/>
              </s:param>
              </s:url>
             <s:a href="%{displaynews}">
                <s:property value="headline"/>
             </s:a>
              </td>
               <td><s:date name="create_time" format="yyyy-MM-dd HH:mm" /></td>
              <td>
             <s:url action="updatenews" var="updatenews">
              <s:param name="id">
              <s:property value="newid"/>
              </s:param>
             </s:url>
             <s:a href="%{updatenews}">修改新闻</s:a>      
                <br/>       
              [url=javascript:deleteproduct(<s:property value='newid'/>);]删除新闻[/url]
             <br/>
                </td>
                </tr>
                </s:iterator>
                          </table>
                        </div>
                        </div>
                        <div class="yema">
                              [list]
                                <%
                  int orderCount = (Integer)request.getAttribute("intPageCount");   //总页数%>
                                  <li class="xiao">共<%=orderCount%>页</li>  
                                  <s:url id="url_pre" value="listnews.html">   
                                     <s:param name="page" value="page-1"></s:param>  
                                  </s:url>   
  
                                  <s:url id="url_next" value="listnews.html">   
                                     <s:param name="page" value="page+1"></s:param>   
                                  </s:url>     
                                 <s:if test="page == 1">
                                 <li class="te"> <s:a href="#"><font color="gray">上一页</font></s:a></li>
                                 </s:if>
                                 <s:else>
                                  <li class="te"> <s:a href="%{url_pre}">上一页</s:a></li>
                                 </s:else>    
                            <%
                  int pageIndex = (Integer)request.getAttribute("page");   //当前页
                  int index = (Integer)request.getAttribute("index");
                  int max = (Integer)request.getAttribute("max");
                     for(int i = index; i <= max; i++){
                         if(i<=0){
                          i=1;
                         }
                      int pageID = (Integer)request.getAttribute("page");
                      if(pageID == i){
                      %>
                      [*][url=listnews.html?page=<%=i %>]<font color="#98AB6F"><%=i %></font>[/url]

                      <%}else{
                %>
                      [*][url=listnews.html?page=<%=i %>]<%=i %>[/url]

         
                <%
                     }
   
                     }
                %>    
                                  <s:if test="page == k">
                                  <li class="te"> <s:a href="#"><font color="gray">下一页</font></s:a></li>
                                 </s:if>
                                 <s:else>
                                  <li class="te"><s:a href="%{url_next}">下一页</s:a></li>
                                  </s:else>
                                  
                                  
                                  <li class="tiao">跳到第 <input type="text" id="tiaozhuan" class="ye" /> 页</li>
                                  <input type="button" value="确定"  style="width:40px;height:21px;font-size:12px;text-align:center;" id="tiaobutton"/>
                                  <br/>
                              [/list]
                        </div>

java后台与 jsp页面的分页案例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号