Servlet 分页

public class APPSettingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int curPage=1;
		int pageSize=8;
		if(null!=request.getParameter("curPage")){
			curPage=Integer.parseInt(request.getParameter("curPage"));
		}
		QueryRunner qr = DBHelper.getQueryRunner();
		String sql = 
				"SELECT staff_mst.login_name,client_mst.name ,login_key_t.intime,login_key_t.expire"+
				"\n FROM login_key_t"+
				"\n INNER JOIN staff_mst USING(staff_id)"+
				"\n INNER JOIN client_mst ON login_key_t.client_id=client_mst.client_id limit "+pageSize+" offset "+(curPage-1)*pageSize;
		String sqlCnt = 
			"SELECT count(*) AS cnts"+
			"\n FROM login_key_t"+
			"\n INNER JOIN staff_mst USING(staff_id)"+
			"\n INNER JOIN client_mst ON login_key_t.client_id=client_mst.client_id";
		ResultSetHandler<Cnt> h = new BeanHandler<Cnt>(Cnt.class);
	
		List<Staff> staffList=null;
		int totalPage=0;
		try {
			 
			 staffList = qr.query(sql, new BeanListHandler<Staff>(Staff.class));
			 Cnt cnts = qr.query(sqlCnt, h);
			 int total = cnts.getCnts();
			 totalPage=(total/pageSize+((total%pageSize)>0?1:0));
			 
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("staffList", staffList);
		request.setAttribute("curpage", curPage);
		request.setAttribute("total", totalPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("../WEB-INF/jsp/setting.jsp"); 
		dispatcher .forward(request, response); 
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
<div class="row">
		<div class="span8 ">
			<table class="table table-bordered">
				<thead>
					<tr >
						<th>登录名称</th>
						<th>所属公司</th>
						<th>登录日期</th>
						<th>有效期</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${staffList }" var="staff">
						<tr >
							<td>${staff.login_name}</td>
							<td>${staff.name}</td>
							<td>${staff.intime}</td>
							<td>${staff.expire}</td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
			<c:forEach begin="1" end="${total}" step="1" var="pageIndex">
				<c:choose>
					<c:when test="${pageIndex eq curpage}">
						<button class="btn disabled" >${pageIndex}</button>
					</c:when>
					<c:otherwise>
						<a class="btn" href="?curPage=${pageIndex}">${pageIndex}</a>
					</c:otherwise>
				</c:choose>
			</c:forEach>
		</div>
	</div>

转载于:https://my.oschina.net/u/561475/blog/104341

单从表现层来说分页不是一个复杂的工作,稍微理一下思路,处于不同competence level的同学应该都能自己搞出来。 以上面的文章列表分页为例,我觉得分页有两点重要的, 一是:分页我们必须首先自己搞清楚,文章总数、每页显示文章数(页大小)、页数 二是:如何做好页脚的分页导航条 实际应用中,文章总数这个值我们从数据库可以得到;每页显示的文章数即分页的页大小可以自己定义;页数我们可以通过下面的个表达式简单得出。 假设: int pageSize = 10; //分页大小 int totalPosts = PagingDAO.entryList.size(); //总文章数 int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数 每页的文章怎么取出来? 知道分页的大小之后,我们生成了页好的选取下拉框,每次选择第几页的时候,都会向Servlet传递当前选择页号的参数,这样Servlet调用后面的DAO相应的方法,取得文章列表信息,再回传到JSP以供显示。 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> page Size : ${pageSize} <br /> Total Posts: ${totalPosts} <br /> Total Pages: ${totalPages} <br /> Current Page: ${pageNumber} <hr /> <table> <thead> <tr align="center"> <td width="10%">Article ID</td> <td width="70%">Article Title</td> <td colspan="3">Actions</td> </tr> </thead> <tbody> <c:forEach items="${entryList}" var="entry"> <tr align="center"> <td>${entry.entryID}</td> <td>${entry.title}</td> <td><a href="viewEntry?entryID=${entry.entryID}">View</a></td> <td><a href="editEntry?entryID=${entry.entryID}">Edit</a></td> <td><a href="deleteEntry?entryID=${entry.entryID}">Delete</a></td> </tr> </c:forEach> </tbody> <tfoot> <tr align="center"> <td colspan="5"> <jsp:include page="paging_footer.jsp"></jsp:include> </td> </tr> </tfoot> </table> <hr/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值