java Web 分页技术总结

  1. public class SplitPage {  
  2.     // 声明一些常量  
  3.     final public static String FIRSTPAGE = "first";// 请求的是首页  
  4.     final public static String PREVIOUSPAGE = "previous";// 请求上一页  
  5.     final public static String NEXTPAGE = "next";// 请求下一页  
  6.     final public static String LASTPAGE = "last";// 请求最后一页  
  7.   
  8.     // 声明一些变量  
  9.     private int pageRow = 3;// 每页显示记录数  
  10.     private int totalRow = 0;// 总的记录数,有数据库操作类DAO提供  
  11.     private int currentPage = 1;// 当前的页面  
  12.     private int firstPage = 1;// 首页位置  
  13.     private int totalPage = 1;// 总的页面数,默认为一页  
  14.   
  15.     public int getPageRow() {  
  16.         return pageRow;  
  17.     }  
  18.   
  19.     /**  
  20.      * 重新设置每页显示的记录数  
  21.      *   
  22.      * @param pageRow  
  23.      *            新的每页显示记录数  
  24.      */  
  25.     public void setPageRow(int pageRow) {  
  26.         if (pageRow == 0) {  
  27.             try {  
  28.                 throw new Exception();// 如果pageRow被设置为零,应当抛出异常.  
  29.             } catch (Exception e) {  
  30.                 // TODO Auto-generated catch block  
  31.                 e.printStackTrace();  
  32.             }  
  33.         }  
  34.   
  35.         this.pageRow = pageRow;// 修改每页的记录数  
  36.         this.totalPage = this.totalRow / this.pageRow  
  37.                 + ((this.totalRow % this.pageRow == 0) ? 0 : 1);  
  38.     }  
  39.   
  40.     public int getTotalRow() {  
  41.         return totalRow;  
  42.     }  
  43.   
  44.     public void setTotalRow(int totalRow) {// 设置分页对象的总记录属性后,就应该根据每页面显示记录数,计算得到总的页面数  
  45.         this.totalRow = totalRow;  
  46.         this.totalPage = this.totalRow/this.pageRow+((this.totalRow%this.pageRow==0)?0:1);  
  47.         System.out.println("当前页"+this.currentPage);  
  48.   
  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 getFirstPage() {  
  60.         return firstPage;  
  61.     }  
  62.   
  63.     public void setFirstPage(int firstPage) {  
  64.         this.firstPage = firstPage;  
  65.     }  
  66.   
  67.     public int getTotalPage() {  
  68.         return totalPage;  
  69.     }  
  70.     //不应该提供方法设置总页面数,它是计算得到的   
  71.   
  72.     /**  
  73.      * 根据请求的标示符参数重新计算要现实的页面  
  74.      *   
  75.      * @param flag  
  76.      *            请求转向的页面标示符  
  77.      * @return int 返回新页  
  78.      */  
  79.     public int toNewPage(String flag) {  
  80.         int newPage = this.currentPage;  
  81.         if (flag != null && !"".equals(flag)) {  
  82.             if (SplitPage.FIRSTPAGE.equals(flag)) {  
  83.                 newPage = 1;  
  84.             } else if (SplitPage.LASTPAGE.equals(flag)) {  
  85.                 newPage = this.totalPage;  
  86.             } else if (SplitPage.NEXTPAGE.equals(flag)) {  
  87.                 newPage = this.currentPage  
  88.                         + ((this.currentPage == this.totalPage) ? 0 : 1);// 如果当前页面与总的页面数相等则不再向后(+1)  
  89.             } else if (SplitPage.PREVIOUSPAGE.equals(flag)) {  
  90.                 newPage = this.currentPage  
  91.                         - ((this.currentPage == this.firstPage) ? 0 : 1);// 如果当前页面与首页相等则不再向前(-1)  
  92.             } else {  
  93.                 // 传入的是个数字字符串参数  
  94.                 newPage = Integer.parseInt(flag.trim());  
  95.             }  
  96.         } else {// 请求的参数为空,则当前页码不变  
  97.             newPage = this.currentPage;  
  98.         }  
  99.         this.setCurrentPage(newPage);// 记得重新设置当期页面  
  100.         return newPage;  
  101.     }  
  102. }  
  1. public interface PageInterface {  
  2.     /** 
  3.      * 查询所有的记录,调用分页生成器类中的分页方法查询数据 
  4.      *  
  5.      * @param splitPage 
  6.      *            分页对象 
  7.      * @return List<Object> 
  8.      * */  
  9.     public List<UserInfoVo> findUserAll(SplitPage splitPage);//查询用户列表   
  10.     /**  
  11.      * 查询所有的记录,调用分页生成器类中的分页方法查询数据  
  12.      *   
  13.      * @param splitPage  
public interface PageInterface {
	/**
	 * 查询所有的记录,调用分页生成器类中的分页方法查询数据
	 * 
	 * @param splitPage
	 *            分页对象
	 * @return List<Object>
	 * */
	public List<UserInfoVo> findUserAll(SplitPage splitPage);//查询用户列表
	/**
	 * 查询所有的记录,调用分页生成器类中的分页方法查询数据
	 * 
	 * @param splitPage
  1.            *            分页对象  
  2.      * @return List<Object>  
  3.      * */  
  4.     public List<ForumInfoVo> findForumAll(SplitPage splitPage);//查询帖子   
  5.     /** 
  6.      * 提供总的记录数 
  7.      * */  
  8.   
  9.     public int getTotalRows();  
  10.     /** 
  11.      * 查询所有的记录,调用分页生成器类中的分页方法查询数据 
  12.      *  
  13.      * @param splitPage 
  14.      *            分页对象 
  15.      * @return List<Object> 
  16.      * */  
  17.     public List<ReforumInfoVo> findReforumAll(SplitPage splitPage);  
  18. }  
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsf/core"%>  
  3. <%@page import="com.weiyi.bbs.dao.ForumInfoDAO"%>  
  4. <%@page import="com.weiyi.bbs.util.*"%>  
  5. <%@page import="com.weiyi.bbs.domain.*"%>  
  6. <%  
  7.     String path = request.getContextPath();  
  8.     String basePath = request.getScheme() + "://"  
  9.             + request.getServerName() + ":" + request.getServerPort()  
  10.             + path + "/";  
  11. %>  
  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  13. <html>  
  14.     <head>  
  15.         <base href="<%=basePath%>">  
  16.   
  17.         <title>帖子列表</title>  
  18.   
  19.         <meta http-equiv="pragma" content="no-cache">  
  20.         <meta http-equiv="cache-control" content="no-cache">  
  21.         <meta http-equiv="expires" content="0">  
  22.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  23.         <meta http-equiv="description" content="This is my page">  
  24.         <script type="text/javascript" src="js/common.js" charset="utf-8"></script>  
  25.         <script type="text/javascript" charset="utf-8">  
  26.         function goPage(){  
  27.            var v = document.getElementById("selectPage").value;  
  28.            var u = document.getElementById("userId").value;  
  29.            window.location.href="forumList.jsp?flag="+v+"&userId="+u;  
  30.         }  
  31.         </script>  
  32.     </head>  
  33.     <!--spage分页对象,保存分页的详细信息 ,存在Session中,每次查询或者是分页数据时只要设置此对象的当前页 -->  
  34.     <!-- dao帖子操作类,主要用来获取总记录数-->  
  35.     <jsp:useBean id="spage" class="com.weiyi.bbs.util.SplitPage"  
  36.         scope="session"></jsp:useBean>  
  37.     <jsp:useBean id="dao" class="com.weiyi.bbs.dao.ForumInfoDAO"  
  38.         scope="session"></jsp:useBean>  
  39.     <%  
  40.         String flag = request.getParameter("flag");  
  41.         int newPage = spage.toNewPage(flag.trim());  
  42.         dao.setTotalRows(dao.getAllRecord());  
  43.         int totalRows = dao.getTotalRows();  
  44.         spage.setTotalRow(totalRows);  
  45.     %>  
  46.     <body>  
  47.         <div id="wrap">  
  48.             <h3>  
  49.                 查看帖子列表  
  50.             </h3>  
  51.             <input type="hidden" name="userId" id="userId" value="<%=request.getParameter("userId")%>"/>  
  52.             <a href="destroyservlet">退出</a>     
  53.             <a  
  54.                 href="forumcontroller?type=toNewForum&userId=<%=request.getParameter("userId")%>&power=user">发新帖</a>  
  55.             <table border="border">  
  56.                 <thead>  
  57.                     <tr>  
  58.                         <th>  
  59.                             ID  
  60.                         </th>  
  61.                         <th>  
  62.                             标题  
  63.                         </th>  
  64.                         <th>  
  65.                             发帖人  
  66.                         </th>  
  67.                         <th>  
  68.                             发帖时间  
  69.                         </th>  
  70.                         <th>  
  71.                             操作  
  72.                         </th>  
  73.                     </tr>  
  74.                 </thead>  
  75.                 <tbody>  
  76.                     <%  
  77.                         List<ForumInfoVo> list = dao.findForumAll(spage);  
  78.                         for (ForumInfoVo forum : list) {  
  79.                     %>  
  80.                     <tr>  
  81.                         <td><%=forum.getId()%></td>  
  82.                         <td><%=forum.getTitle()%></td>  
  83.                         <td><%=forum.getAuthorName()%></td>  
  84.                         <td><%=forum.getCreateTime()%></td>  
  85.                         <td>  
  86.                             <a  
  87.                                 href="forumcontroller?type=look&power=user&userId=<%=request.getParameter("userId")%>&id=<%=forum.getId()%>">查看</a>    
  88.                         </td>  
  89.                     </tr>  
  90.                     <%  
  91.                         }  
  92.                     %>  
  93.                 </tbody>  
  94.             </table>  
  95.             <div id="foot">  
  96.                 <a href="forumList.jsp?flag=<%=SplitPage.FIRSTPAGE%>&userId=<%=request.getParameter("userId")%>">首页</a>   
  97.                 <a href="forumList.jsp?flag=<%=SplitPage.PREVIOUSPAGE%>&userId=<%=request.getParameter("userId")%>">上一页</a>   
  98.                 <a href="forumList.jsp?flag=<%=SplitPage.NEXTPAGE%>&userId=<%=request.getParameter("userId")%>">下一页</a>   
  99.                 <a href="forumList.jsp?flag=<%=SplitPage.LASTPAGE%>&userId=<%=request.getParameter("userId")%>">末页</a>   
  100.                 <select id="selectPage" name="selectPage" οnchange="goPage();">  
  101.                     <%  
  102.                         for (int i = 1; i <= spage.getTotalPage(); ++i) {  
  103.                     %>  
  104.                     <option value="<%=i%>"  
  105.                         <%=(spage.getCurrentPage() == i) ? "selected='selected'"  
  106.                         : ""%> ><%=i%></option>  
  107.                         <%  
  108.                             }  
  109.                         %>  
  110.                       
  111.                 </select>  
  112.                 <label><%=spage.getCurrentPage()%>/<%=spage.getTotalPage()%>页  
  113.                 </label>  
  114.             </div>  
  115.         </div>  
  116.     </body>  
  117. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsf/core"%>
<%@page import="com.weiyi.bbs.dao.ForumInfoDAO"%>
<%@page import="com.weiyi.bbs.util.*"%>
<%@page import="com.weiyi.bbs.domain.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>帖子列表</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<script type="text/javascript" src="js/common.js" charset="utf-8"></script>
		<script type="text/javascript" charset="utf-8">
		function goPage(){
		   var v = document.getElementById("selectPage").value;
		   var u = document.getElementById("userId").value;
		   window.location.href="forumList.jsp?flag="+v+"&userId="+u;
		}
		</script>
	</head>
	<!--spage分页对象,保存分页的详细信息 ,存在Session中,每次查询或者是分页数据时只要设置此对象的当前页 -->
	<!-- dao帖子操作类,主要用来获取总记录数-->
	<jsp:useBean id="spage" class="com.weiyi.bbs.util.SplitPage"
		scope="session"></jsp:useBean>
	<jsp:useBean id="dao" class="com.weiyi.bbs.dao.ForumInfoDAO"
		scope="session"></jsp:useBean>
	<%
		String flag = request.getParameter("flag");
		int newPage = spage.toNewPage(flag.trim());
		dao.setTotalRows(dao.getAllRecord());
		int totalRows = dao.getTotalRows();
		spage.setTotalRow(totalRows);
	%>
	<body>
		<div id="wrap">
			<h3>
				查看帖子列表
			</h3>
			<input type="hidden" name="userId" id="userId" value="<%=request.getParameter("userId")%>"/>
			<a href="destroyservlet">退出</a>   
			<a
				href="forumcontroller?type=toNewForum&userId=<%=request.getParameter("userId")%>&power=user">发新帖</a>
			<table border="border">
				<thead>
					<tr>
						<th>
							ID
						</th>
						<th>
							标题
						</th>
						<th>
							发帖人
						</th>
						<th>
							发帖时间
						</th>
						<th>
							操作
						</th>
					</tr>
				</thead>
				<tbody>
					<%
						List<ForumInfoVo> list = dao.findForumAll(spage);
						for (ForumInfoVo forum : list) {
					%>
					<tr>
						<td><%=forum.getId()%></td>
						<td><%=forum.getTitle()%></td>
						<td><%=forum.getAuthorName()%></td>
						<td><%=forum.getCreateTime()%></td>
						<td>
							<a
								href="forumcontroller?type=look&power=user&userId=<%=request.getParameter("userId")%>&id=<%=forum.getId()%>">查看</a>  
						</td>
					</tr>
					<%
						}
					%>
				</tbody>
			</table>
			<div id="foot">
				<a href="forumList.jsp?flag=<%=SplitPage.FIRSTPAGE%>&userId=<%=request.getParameter("userId")%>">首页</a> 
				<a href="forumList.jsp?flag=<%=SplitPage.PREVIOUSPAGE%>&userId=<%=request.getParameter("userId")%>">上一页</a> 
				<a href="forumList.jsp?flag=<%=SplitPage.NEXTPAGE%>&userId=<%=request.getParameter("userId")%>">下一页</a> 
				<a href="forumList.jsp?flag=<%=SplitPage.LASTPAGE%>&userId=<%=request.getParameter("userId")%>">末页</a> 
				<select id="selectPage" name="selectPage" οnchange="goPage();">
					<%
						for (int i = 1; i <= spage.getTotalPage(); ++i) {
					%>
					<option value="<%=i%>"
						<%=(spage.getCurrentPage() == i) ? "selected='selected'"
						: ""%> ><%=i%></option>
						<%
							}
						%>
					
				</select>
				<label><%=spage.getCurrentPage()%>/<%=spage.getTotalPage()%>页
				</label>
			</div>
		</div>
	</body>
</html>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值