码迷,mamicode.com
首页 > 其他好文 > 详细

新闻发布系统:分页

时间:2016-07-06 18:14:48      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

分页实现:

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

①确定每页显示的总页数

②计算显示的总页数

③编写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() %>]&nbsp; <a href="<%=path %>/servlet/NewsServlet?pageIndex=<%=page2.getPageIndex()+1%>">下一页</a> <a href="#">末页</a> </p>
      </ul>
技术分享

 

新闻发布系统:分页

标签:

原文地址:http://www.cnblogs.com/hr1997/p/5647347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!