在JSP中使用Struts2标签分页 限制页码只显示10页 带分页算法 样式

之前做分页效果一直都是使用JS控制,后来觉得不方便也不好控制,就自己写了这个根据Struts2标签在JSP页面中控制页码显示的分页。

分页效果图:




废话不多说,直接上代码了:

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 PagerAction extends StrutsBaseAction {  
  15.     // 当前页  
  16.     private int currentPage = 1;  
  17.     // 也容量  
  18.     private int pageSize = 10;  
  19.     // 总页数  
  20.     private int totalPage;  
  21.     // 总记录数  
  22.     private int allCount = 1000;  
  23.   
  24.     @Action(value = "pager", results = { @Result(name = "success", location = "/pager.jsp") }, params = {  
  25.             "contentType""text/html" })  
  26.     public String pager() {  
  27.         System.out.println(currentPage);  
  28.         totalPage = allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1;  
  29.         return SUCCESS;  
  30.     }  
  31.   
  32.     public int getCurrentPage() {  
  33.         return currentPage;  
  34.     }  
  35.   
  36.     public void setCurrentPage(int currentPage) {  
  37.         this.currentPage = currentPage;  
  38.     }  
  39.   
  40.     public int getTotalPage() {  
  41.         return totalPage;  
  42.     }  
  43.   
  44.     public void setTotalPage(int totalPage) {  
  45.         this.totalPage = totalPage;  
  46.     }  
  47.   
  48.     public int getAllCount() {  
  49.         return allCount;  
  50.     }  
  51.   
  52. }  
package com.goldweb.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;
import com.goldweb.common.StrutsBaseAction;

@SuppressWarnings("serial")
@Controller
@ParentPackage("json-default")
@Namespace("/pager")
public class PagerAction extends StrutsBaseAction {
	// 当前页
	private int currentPage = 1;
	// 也容量
	private int pageSize = 10;
	// 总页数
	private int totalPage;
	// 总记录数
	private int allCount = 1000;

	@Action(value = "pager", results = { @Result(name = "success", location = "/pager.jsp") }, params = {
			"contentType", "text/html" })
	public String pager() {
		System.out.println(currentPage);
		totalPage = allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1;
		return SUCCESS;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getAllCount() {
		return allCount;
	}

}

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>  
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分页测试</title>
<style type="text/css">
/* 全局样式 */
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
	{
	margin: 0px auto;
	padding: 0px;
	border: 0;
}

body {
	font-size: 12px;
	font-family: Tahoma, Geneva, sans-serif;
	background-position: top;
	background-repeat: repeat-x;
}

a {
	text-decoration: none;
}
/* 正文:分页功能区 */
#pages {
	width: 940px;
	text-align: center;
	height: 28px;
	line-height: 28px;
	margin-top: 5px;
}

#pages a,#pages a.current_page:hover {
	padding: 5px 10px;
}

#pages a:hover {
	padding: 5px 9px;
}
/* 正文:分页功能区 */
#pages a {
	border-radius: 4px;
	color: #000;
	border: #97b9c9 solid 1px;
}

#pages a:hover {
	background: #cddde4;
	border: #97b9c9 solid 1px;
	color: #067db5;
}

#pages a.current_page {
	background: #cddde4;
	border: #89bdd8 solid 1px;
	color: #067db5;
}
</style>
</head>
<body>

	<fieldset style="margin: 0 auto;">
		<legend>分页测试</legend>
		<div id="pages">
			<s:if test="currentPage<=1">
				<a href="#">首页</a>
				<a href="#" class="pager">上一页</a>
			</s:if>
			<s:else>
				<a href="pager.action?currentPage=1">首页</a>
				<a
					href="pager.action?currentPage=<s:property value="currentPage-1"/>">上一页</a>
			</s:else>
			<!-- 页码限制为10页算法 -->
			<s:iterator
				begin="currentPage>4?currentPage+5>totalPage?totalPage>10?totalPage-9:1:currentPage-4:1"
				end="currentPage>4?currentPage+5>totalPage?totalPage:currentPage+5:totalPage>10?10:totalPage"
				var="i">
				<s:if test="currentPage==#i">
					<a href="#" class="current_page"><s:property value="#i" /></a>
				</s:if>
				<s:else>
					<a href="pager.action?currentPage=<s:property value="#i"/>"><s:property
							value="#i" /></a>
				</s:else>
			</s:iterator>
			<s:if test="currentPage>=totalPage">
				<a href="#">下一页</a>
				<a href="#">尾页</a>
			</s:if>
			<s:else>
				<a
					href="pager.action?currentPage=<s:property value="currentPage+1"/>">下一页</a>
				<a href="pager.action?currentPage=<s:property value="totalPage"/>">尾页</a>
			</s:else>
		</div>
	</fieldset>

</body>
</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. }  
package com.goldweb.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;
import com.goldweb.common.StrutsBaseAction;

@SuppressWarnings("serial")
@Controller
@ParentPackage("json-default")
@Namespace("/pager")
public class Pager2Action extends StrutsBaseAction {
	// 当前页
	private int currentPage = 1;
	// 页容量
	private int pageSize = 10;
	// 总页数
	private int totalPage;
	// 总记录数
	private int allCount = 666;
	// 开始页码
	private int startPage = 1;
	// 结束页码
	private int endPage = 10;

	@Action(value = "pager2", results = { @Result(name = "success", location = "/pager2.jsp") }, params = {
			"contentType", "text/html" })
	public String pager() {
		System.out.println(currentPage);
		totalPage = (int) Math.ceil((double) allCount / (double) pageSize);
		// 显示页码计算
		if (currentPage > 4) {
			startPage = currentPage - 4;
			endPage = currentPage + 5;
		}
		if (endPage > totalPage) {
			if (totalPage>10)
				startPage = totalPage - 9;
			else
				startPage = 1;
			endPage = totalPage;
		}
		if (startPage < 1) {
			startPage = 1;
		}
		return SUCCESS;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getAllCount() {
		return allCount;
	}

	public int getStartPage() {
		return startPage;
	}

	public int getEndPage() {
		return endPage;
	}

}

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>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值