jsp struts2 標籤實現分頁

第一種

java 代碼

  1. package com.action;  
  2.   
  3. import org.apache.struts2.convention.annotation.Action;  
  4. import org.apache.struts2.convention.annotation.Namespace;  
  5. import org.apache.struts2.convention.annotation.ParentPackage;  
  6. import org.apache.struts2.convention.annotation.Result;  
  7. import org.springframework.stereotype.Controller;  
  8. import com.goldweb.common.StrutsBaseAction;  
  9.   

  10. public class PagerAction extends StrutsBaseAction {  
  11.     // 當前頁  
  12.     private int currentPage = 1;  
  13.     // 也容量  
  14.     private int pageSize = 10;  
  15.     // 總頁數  
  16.     private int totalPage;  
  17.     // 總記錄數  
  18.     private int allCount = 1000;  
  19.   
  20.     public String pager() {  
  21.         System.out.println(currentPage);  
  22.         totalPage = allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1;  
  23.         return SUCCESS;  
  24.     }  
  25.   
  26.     public int getCurrentPage() {  
  27.         return currentPage;  
  28.     }  
  29.   
  30.     public void setCurrentPage(int currentPage) {  
  31.         this.currentPage = currentPage;  
  32.     }  
  33.   
  34.     public int getTotalPage() {  
  35.         return totalPage;  
  36.     }  
  37.   
  38.     public void setTotalPage(int totalPage) {  
  39.         this.totalPage = totalPage;  
  40.     }  
  41.   
  42.     public int getAllCount() {  
  43.         return allCount;  
  44.     }  
  45.   
  46. }  
jsp 頁面

  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>分頁測試</title>  
  8. <style type="text/css">  
  9. /* 全局樣式 */  
  10. body,p,div,ul,ol,li,dl,dd,dt,h1,h2,h3,h4,h5,h6,form,input,select,label,table,tr,td,th,thead,tbody,tfoot  
  11.     {  
  12.     margin: 0px auto;  
  13.     padding: 0px;  
  14.     border: 0;  
  15. }  
  16.   
  17. body {  
  18.     font-size: 12px;  
  19.     font-family: Tahoma, Geneva, sans-serif;  
  20.     background-position: top;  
  21.     background-repeat: repeat-x;  
  22. }  
  23.   
  24. a {  
  25.     text-decoration: none;  
  26. }  
  27. /* 正文:分頁功能區 */  
  28. #pages {  
  29.     width: 940px;  
  30.     text-align: center;  
  31.     height: 28px;  
  32.     line-height: 28px;  
  33.     margin-top: 5px;  
  34. }  
  35.   
  36. #pages a,#pages a.current_page:hover {  
  37.     padding: 5px 10px;  
  38. }  
  39.   
  40. #pages a:hover {  
  41.     padding: 5px 9px;  
  42. }  
  43. /* 正文:分頁功能區 */  
  44. #pages a {  
  45.     border-radius: 4px;  
  46.     color: #000;  
  47.     border: #97b9c9 solid 1px;  
  48. }  
  49.   
  50. #pages a:hover {  
  51.     background: #cddde4;  
  52.     border: #97b9c9 solid 1px;  
  53.     color: #067db5;  
  54. }  
  55.   
  56. #pages a.current_page {  
  57.     background: #cddde4;  
  58.     border: #89bdd8 solid 1px;  
  59.     color: #067db5;  
  60. }  
  61. </style>  
  62. </head>  
  63. <body>  
  64.   
  65.     <fieldset style="margin: 0 auto;">  
  66.         <legend>分頁測試</legend>  
  67.         <div id="pages">  
  68.             <s:if test="currentPage<=1">  
  69.                 <a href="#">首頁</a>  
  70.                 <a href="#" class="pager">上一頁</a>  
  71.             </s:if>  
  72.             <s:else>  
  73.                 <a href="pager.action?currentPage=1">首頁</a>  
  74.                 <a  
  75.                     href="pager.action?currentPage=<s:property value="currentPage-1"/>">上一頁</a>  
  76.             </s:else>  
  77.             <!-- 頁碼限制爲10頁算法 -->  
  78.             <s:iterator  
  79.                 begin="currentPage>4?currentPage+5>totalPage?totalPage>10?totalPage-9:1:currentPage-4:1"  
  80.                 end="currentPage>4?currentPage+5>totalPage?totalPage:currentPage+5:totalPage>10?10:totalPage"  
  81.                 var="i">  
  82.                 <s:if test="currentPage==#i">  
  83.                     <a href="#" class="current_page"><s:property value="#i" /></a>  
  84.                 </s:if>  
  85.                 <s:else>  
  86.                     <a href="pager.action?currentPage=<s:property value="#i"/>"><s:property  
  87.                             value="#i" /></a>  
  88.                 </s:else>  
  89.             </s:iterator>  
  90.             <s:if test="currentPage>=totalPage">  
  91.                 <a href="#">下一頁</a>  
  92.                 <a href="#">尾頁</a>  
  93.             </s:if>  
  94.             <s:else>  
  95.                 <a  
  96.                     href="pager.action?currentPage=<s:property value="currentPage+1"/>">下一頁</a>  
  97.                 <a href="pager.action?currentPage=<s:property value="totalPage"/>">尾頁</a>  
  98.             </s:else>  
  99.         </div>  
  100.     </fieldset>  
  101.   
  102. </body>  
  103. </html>  
以上是通過Struts2標籤在JSP中限制顯示的頁碼。


以下通過在Java的Action中計算好開始頁碼和結束頁碼來限制顯示的頁碼:


Java代碼:

  1. package com.goldweb.action;  
  2.   
  3. import org.apache.struts2.convention.annotation.Action;  
  4. import org.apache.struts2.convention.annotation.Namespace;  
  5. import org.apache.struts2.convention.annotation.ParentPackage;  
  6. import org.apache.struts2.convention.annotation.Result;  
  7. import org.springframework.stereotype.Controller;  
  8. import com.goldweb.common.StrutsBaseAction;  
  9.   
  10. @SuppressWarnings("serial")  
  11. @Controller  
  12. @ParentPackage("json-default")  
  13. @Namespace("/pager")  
  14. public class Pager2Action extends StrutsBaseAction {  
  15.     // 當前頁  
  16.     private int currentPage = 1;  
  17.     // 頁容量  
  18.     private int pageSize = 10;  
  19.     // 總頁數  
  20.     private int totalPage;  
  21.     // 總記錄數  
  22.     private int allCount = 666;  
  23.     // 開始頁碼  
  24.     private int startPage = 1;  
  25.     // 結束頁碼  
  26.     private int endPage = 10;  
  27.   
  28.     @Action(value = "pager2", results = { @Result(name = "success", location = "/pager2.jsp") }, params = {  
  29.             "contentType""text/html" })  
  30.     public String pager() {  
  31.         System.out.println(currentPage);  
  32.         totalPage = (int) Math.ceil((double) allCount / (double) pageSize);  
  33.         // 顯示頁碼計算  
  34.         if (currentPage > 4) {  
  35.             startPage = currentPage - 4;  
  36.             endPage = currentPage + 5;  
  37.         }  
  38.         if (endPage > totalPage) {  
  39.             if (totalPage>10)  
  40.                 startPage = totalPage - 9;  
  41.             else  
  42.                 startPage = 1;  
  43.             endPage = totalPage;  
  44.         }  
  45.         if (startPage < 1) {  
  46.             startPage = 1;  
  47.         }  
  48.         return SUCCESS;  
  49.     }  
  50.   
  51.     public int getCurrentPage() {  
  52.         return currentPage;  
  53.     }  
  54.   
  55.     public void setCurrentPage(int currentPage) {  
  56.         this.currentPage = currentPage;  
  57.     }  
  58.   
  59.     public int getTotalPage() {  
  60.         return totalPage;  
  61.     }  
  62.   
  63.     public void setTotalPage(int totalPage) {  
  64.         this.totalPage = totalPage;  
  65.     }  
  66.   
  67.     public int getAllCount() {  
  68.         return allCount;  
  69.     }  
  70.   
  71.     public int getStartPage() {  
  72.         return startPage;  
  73.     }  
  74.   
  75.     public int getEndPage() {  
  76.         return endPage;  
  77.     }  
  78.   
  79. }  
jsp 頁面

  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>分頁測試</title>  
  8. <style type="text/css">  
  9. /* 全局樣式 */  
  10. body, p, div, ul, ol, li, dl, dd, dt, h1, h2, h3, h4, h5, h6, form, input, select, label, table, tr, td, th, thead, tbody, tfoot  
  11. {  
  12.     margin: 0px auto;  
  13.     padding: 0px;  
  14.     border: 0;  
  15. }  
  16. body {  
  17.     font-size: 12px;  
  18.     font-family: Tahoma,Geneva,sans-serif;  
  19.     background-position: top;  
  20.     background-repeat: repeat-x;  
  21. }  
  22. a {  
  23.     text-decoration: none;  
  24. }  
  25. /* 正文:分頁功能區 */  
  26. #pages {  
  27.     width: 940px;  
  28.     text-align: center;  
  29.     height: 28px;  
  30.     line-height: 28px;  
  31.     margin-top: 5px;  
  32. }  
  33. #pages a, #pages a.current_page:hover {  
  34.     padding: 5px 10px;  
  35. }  
  36. #pages a:hover {  
  37.     padding: 5px 9px;  
  38. }  
  39. /* 正文:分頁功能區 */  
  40. #pages a {  
  41.     border-radius: 4px;  
  42.     color: #000;  
  43.     border: #97b9c9 solid 1px;  
  44. }  
  45. #pages a:hover {  
  46.     background: #cddde4;  
  47.     border: #97b9c9 solid 1px;  
  48.     color: #067db5;  
  49. }  
  50. #pages a.current_page {  
  51.     background: #cddde4;  
  52.     border: #89bdd8 solid 1px;  
  53.     color: #067db5;  
  54. }  
  55. </style>  
  56. </head>  
  57. <body>  
  58.       
  59. <fieldset style="margin:0 auto;">  
  60.     <legend>分頁測試</legend>  
  61.     <div id="pages">  
  62.         <s:if test="currentPage<=1">  
  63.             <a href="#">首頁</a>  
  64.             <a href="#" class="pager">上一頁</a>  
  65.         </s:if>  
  66.         <s:else>  
  67.             <a href="pager2.action?currentPage=1">首頁</a>  
  68.             <a href="pager2.action?currentPage=<s:property value="currentPage-1"/>">上一頁</a>  
  69.         </s:else>  
  70.         <!-- 這裏直接用Action傳過來的 開始頁碼 和 結束頁碼 -->  
  71.         <s:iterator begin="startPage" end="endPage" var="i">  
  72.             <s:if test="currentPage==#i">  
  73.                 <a href="#" class="current_page"><s:property value="#i"/></a>  
  74.             </s:if>  
  75.             <s:else>  
  76.                 <a href="pager2.action?currentPage=<s:property value="#i"/>"><s:property value="#i"/></a>  
  77.             </s:else>  
  78.         </s:iterator>  
  79.         <s:if test="currentPage>=totalPage">  
  80.             <a href="#">下一頁</a>  
  81.             <a href="#">尾頁</a>  
  82.         </s:if>  
  83.         <s:else>  
  84.             <a href="pager2.action?currentPage=<s:property value="currentPage+1"/>">下一頁</a>  
  85.             <a href="pager2.action?currentPage=<s:property value="totalPage"/>">尾頁</a>  
  86.         </s:else>  
  87.     </div>  
  88. </fieldset>  
  89.       
  90. </body>  
  91. </html>
效果圖




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章