• 新闻发布系统(分页显示)


    根据上次的新闻发布展示页面效果,进行分页操作:

    分页实现:

    实现数据的分页显示,需要以下几个关键步骤:

    ①确定每页显示的总页数

    ②计算显示的总页数

    ③编写SQL语句

      一.页面效果图

    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() %>]&nbsp; <a href="<%=path %>/servlet/NewsServlet?pageIndex=<%=page2.getPageIndex()+1%>">下一页</a> <a href="#">末页</a> </p>
          </ul>
    复制代码
  • 相关阅读:
    Large-Scale Oil Palm Tree Detection from High-Resolution Satellite Images Using Two-Stage Convolutional Neural Networks(worldview油棕树检测)
    Visual Detail Augmented Mapping for Small Aerial Target Detection(航片动态小目标检测)
    Large Kernel Matters —— Improve Semantic Segmentation by Global Convolutional Network(GCN全局卷积网络)
    Learning a Discriminative Feature Network for Semantic Segmentation(语义分割DFN,区别特征网络)
    Semantic Segmentation on Remotely Sensed Images Using an Enhanced Global Convolutional Network with Channel Attention and Domain Specific Transfer Learning
    Super-Resolution Restoration of MISR Images Using the UCL MAGiGAN System 超分辨率恢复
    python 中内存释放与函数传递numpy数组问题
    hdfs的shell命令
    副本策略
    数据分割
  • 原文地址:https://www.cnblogs.com/hq-123/p/5647332.html
Copyright © 2020-2023  润新知