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>
相關文章
相關標籤/搜索