新闻发布系统<分页>
分页实现:
实现数据的分页显示,需要以下几个关键步骤:
①确定每页显示的总页数
②计算显示的总页数
③编写SQL语句
当点击下一页时地址变成?pageIndex =2:
1、创建util包,Page类,定义相关属性并进行封装:
package cn.news.util; import java.util.List; import cn.news.entity.NewsDetail; public class Page { //当前页 private int pageIndex; //页面记录数 private int pageSize; //本业显示真实数据 private List<NewsDetail> list; //总页数 private int totalPages; //总记录数 private int totalRecords; public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<NewsDetail> getList() { return list; } public void setList(List<NewsDetail> list) { this.list = list; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } }
2、在Dao实现类NewsDetailDaoImpl定义获取新闻当前页的数据的方法getonePage,并植入两个参数pageIndex, pageSize
@Override public List<NewsDetail> getonePage(int pageIndex, int pageSize) throws Exception { List<NewsDetail> list=new ArrayList<NewsDetail>(); String sql="select top "+pageSize+" * from newsDetails where newsId not in(select top "+(pageIndex-1)*pageSize+" newsId from newsDetails)"; ResultSet rs=executeQuery(sql); if(rs!=null) { while(rs.next()) { NewsDetail news=new NewsDetail(); news.setNewsId(rs.getInt("newsId")); news.setNewsTitle(rs.getString("newsTitle")); news.setNewsContent(rs.getString("newsContent")); news.setNewsCreateDate(rs.getDate("newsCreateDate")); news.setNewsAuthor(rs.getString("newsAuthor")); news.setNewsCategoryId(rs.getInt("newsCategoryId")); list.add(news); } } return list; }
3、在NewsServlet类中把数据传递给Jsp页面
package cn.news.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.news.entity.NewsDetail; import cn.news.impl.NewsDetailDaoImpl; import cn.news.util.Page; public class NewsServlet extends HttpServlet { /** */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //实例化dao NewsDetailDaoImpl dao=new NewsDetailDaoImpl(); try { Page page=new Page(); //默认3条数据 int pageSize=3; page.setPageSize(pageSize); //当前页 int myindex=1; String pageIndex=request.getParameter("pageIndex"); if(pageIndex!=null&&(!pageIndex.equals(""))) { myindex=Integer.parseInt(pageIndex); }else { myindex=1; } //当前页赋值 page.setPageIndex(myindex); int mytotalPages=0; int totalPages=dao.getAllCount(); if(totalPages%pageSize==0) { mytotalPages=totalPages/pageSize; } else { mytotalPages=totalPages/pageSize+1; } page.setTotalPages(mytotalPages); //泛型数据 List<NewsDetail> list= dao.getonePage(page.getPageIndex(), pageSize); page.setList(list); request.setAttribute("list", page); request.getRequestDispatcher("/index.jsp").forward(request, response); //List<NewsDetail> list=dao.getAllNews(); } catch (Exception e) { e.printStackTrace(); } } }
4、在index.jsp页面得到泛型集合,并获取当前页数和下一页
<% Page page2=(Page)request.getAttribute("list"); for(NewsDetail item:page2.getList()) { %> <li><a href='newspages/news_read.jsp'><%=item.getNewsTitle() %></a><span><%=item.getNewsCreateDate() %></span></li> <% } %> <p align="right"> 当前页数:[<%=page2.getPageIndex() %>/<%=page2.getTotalPages() %>] <a href="<%=path %>/servlet/NewsServlet?pageIndex=<%=page2.getPageIndex()+1%>">下一页</a> <a href="#">末页</a> </p> </ul>
作者:Monodrama
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。