标签:
实现效果图:
当点击下一页时地址变成?pageIndex =2:
因为是分页,所以可以根据分页的需求定制出页面类代码如图:
package cn.news.util; import java.util.List; import cn.news.entity.NewsDetail; public class Page { //泛型 private List<NewsDetail> list; public List<NewsDetail> getList() { return list; } public void setList(List<NewsDetail> list) { this.list = list; } 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 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; } //当前页 private int pageIndex; //页的大小 private int pageSize; //总页数 private int totalPages; //总记录数 private int totalRecords;
}
在newsdetaildao 数据访问层编写代码:
package cn.news.dao; import java.util.List; import cn.news.entity.NewsDetail; /** * 接口 * @author happy * */ public interface NewsDetailDAO { //1.1 写一个查询所有新闻纪录的方法 public List<NewsDetail> getAllNews() throws Exception; public List<NewsDetail> getOnePageNews(int pageIndex,int pageSize) throws Exception; public int getCount() throws Exception; public int login(NewsDetail news) throws Exception; }
编写其实现类:
package cn.news.dao.imp; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import org.junit.Test; import cn.news.dao.BaseDao; import cn.news.dao.NewsDetailDAO; import cn.news.entity.NewsDetail; public class NewsDetailImpl extends BaseDao implements NewsDetailDAO { public List<NewsDetail> getAllNews() throws Exception { List<NewsDetail> list =new ArrayList<NewsDetail>(); String sql ="select * 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; } public List<NewsDetail> getOnePageNews(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; } public int getCount() throws Exception { String sql ="select count(1) from newsDetails"; ResultSet rs=executeQuery(sql); int totalRecords=0; if(rs!=null){ if(rs.next()){ totalRecords=rs.getInt(1); } } return totalRecords; } public int login(NewsDetail news) throws Exception { // TODO Auto-generated method stub int flag =0; String sql ="select count(1) from newsDetails where newsid =? and newsCategoryId=?"; Object [] paras ={news.getNewsId(),news.getNewsCategoryId()}; ResultSet rs =executeQuery(sql, paras); if(rs!=null){ if(rs.next()){ flag=1; } } return flag; } }
在servlet类中把要显示的数据给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 org.junit.Test; import cn.news.dao.imp.NewsDetailImpl; import cn.news.entity.NewsDetail; 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 { NewsDetailImpl dao=new NewsDetailImpl(); try { Page page =new Page(); int pageSize =3; page.setPageSize(pageSize); String temp =request.getParameter("pageIndex"); int pageIndex ; if(temp!=null){ pageIndex=Integer.parseInt(temp); }else{ pageIndex=1; } page.setPageIndex(pageIndex); //set pagetotal int count =dao.getCount(); page.setTotalRecords(count); int totalPages=0; if(count%pageSize==0){ totalPages=count/pageSize; }else{ totalPages=count/pageSize+1; } page.setTotalPages(totalPages); List<NewsDetail> list = dao.getOnePageNews(pageIndex, pageSize); page.setList(list); request.setAttribute("list", page); } catch (Exception e) { e.printStackTrace(); } request.getRequestDispatcher("/index.jsp").forward(request, response); } }
在jsp页面控制要显示的页面
</ul> <ul class="classlist"> <% Page page2 = (Page)request.getAttribute("list"); //从作用域中取出一个泛型 List<NewsDetail> list= page2.getList(); for(NewsDetail item :list){ //一个item就是一个新闻对象 //item.getNewsTitle(); %> <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="<%=path %>/servlet/newsServlet?pageIndex=<%=page2.getPageIndex()+1 %>">下一页</a> <a href="#">末页</a> </p> </ul>
标签:
原文地址:http://www.cnblogs.com/myhome-1/p/5646723.html