jsp+servlet 分页笔记

jsp页面分页导航条

<form action="NewsListServlet" method="get" id="navigatorForm"
			class="form">
			<a href="NewsListServlet?pageNumber=1">首页</a>
			<c:if test="${pageNumber>1}">
				<a href="NewsListServlet?pageNumber=${pageNumber-1}">上一页</a>
			</c:if>
			跳转到第 <select name="pageNumber" onchange="gotoSelectedPage();">
				<c:forEach begin="1" end="${totalPages}" step="1" var="pageIndex">
					<c:choose>
						<c:when test="${pageIndex eq pageNumber}">
							<option value="${pageIndex}" selected="selected">${pageIndex}</option>
						</c:when>
						<c:otherwise>
							<option value="${pageIndex}">${pageIndex}</option>
						</c:otherwise>
					</c:choose>
				</c:forEach>
			</select>页
			<c:if test="${pageNumber<totalPages}">
				<a href="NewsListServlet?pageNumber=${pageNumber+1}">下一页</a>
			</c:if>
			<a href="NewsListServlet?pageNumber=${totalPages}">末页</a>
		</form>
<script type="text/javascript">
	function gotoSelectedPage() {
		var x = document.getElementById("navigatorForm");
		//alert("Original action: " + x.action)  
		x.submit();
	}
</script>

dao实现层(这是使用的是limit语句)

public List<News> getAllListByPage(int pageNumber, int pageSize){
		List<News> list = new ArrayList<News>(); 
		try {
			con = DB.getConnection();
			ps = con.prepareStatement("select * from news order by date desc limit "+pageNumber+","+pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				News n = new News();
				n.setId(rs.getInt("id"));
				n.setTitle(rs.getString("title"));
				n.setContent(rs.getString("content"));
				n.setType(rs.getInt("type"));
				n.setAuthor(rs.getString("author"));
				n.setDate(rs.getDate("date"));
				list.add(n);
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

servlet层

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置字符集
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		NewsDao nd = new NewsDao();
		
		String pageNumberStr = request.getParameter("pageNumber");   
		int pageNumber = 1;   //当前页 
	    if(pageNumberStr!=null && !pageNumberStr.isEmpty())   
	    {   
	        pageNumber = Integer.parseInt(pageNumberStr);   
	    }  
	    
		int pageSize = 10; //分页大小
		int totalPosts = nd.getAllList().size(); //总文章数
		int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数
		
		List<News> list = nd.getAllListByPage((pageNumber-1)*pageSize, pageSize);
		
		request.setAttribute("pageSize", pageSize);   
	    request.setAttribute("totalPosts", totalPosts);   
	    request.setAttribute("pageNumber", pageNumber);   
	    request.setAttribute("totalPages", totalPages);
	    
		if(list.size()>0){
			request.setAttribute("allNewsList", list);
			request.getRequestDispatcher("/admin/news/allList.jsp").forward(request, response);
		}else {
			out.println("暂时没有数据!");
		}
	}


你可能感兴趣的:(jsp+servlet 分页笔记)