bbs小项目整理(消息分页展现)
Posted 鲤鱼-CC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bbs小项目整理(消息分页展现)相关的知识,希望对你有一定的参考价值。
分页显示发布的消息形成消息列表1.首先创建分页的javabean来装载分页的属性设置
在com.congbbs.javabean创建pageBean对象:
package com.congbbs.javabean; public class PageBean { private int currPage; //当前页数 private int pageSize; //当前显示的记录数 private int totalCount; //总记录数 private int tatalPage; //总页数 public PageBean(){ //无参构造方法 } public PageBean(int currPage, int pageSize, int totalCount, int tatalPage) { //有参构造方法 this.currPage = currPage; this.pageSize = pageSize; this.totalCount = totalCount; this.tatalPage = tatalPage; } //相应的set和get方法 public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTatalPage() { return tatalPage; } public void setTatalPage(int tatalPage) { this.tatalPage = tatalPage; } }
分页的思想:通过(mysql的limit语句) 设置查询起点和要查询的记录数将相应的数据从数据库中查询出来,然后再页面上一页页展现出来
要查询出有多少页的数据当然的前提是要计算出有多少记录数啦(总页数 = 总数据记录数/每页记录数)
总记录数通过查询数据库可以得出, 每页记录数由自己设定 , 当前页由页面传递过来
在上一篇的messageDao中加入相应的方法,在messageDaoImple中对方法进行实现:
package com.congbbs.dao; import java.util.List; import com.congbbs.javabean.Message; import com.congbbs.javabean.PageBean; public interface MessageDao { //添加消息 public void addMessage(Message message); //分页查询记录数 public List<Message> findAllMessage(PageBean pageBean); //获得总记录数 public int findAllCount(); }
package com.congbbs.dao.imple; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import com.congbbs.dao.MessageDao; import com.congbbs.javabean.Message; import com.congbbs.javabean.PageBean; import com.congbbs.util.DbConn; public class MessageDaoImpl implements MessageDao { //插入一条消息 @Override public void addMessage(Message message) { //通过工具类获得数据库的链接 Connection conn = DbConn.getConnection(); //编写sql语句 String sql = "insert into tb_message(messageTitle, messageContent,userID, publishTime)"+ "values(?,?,?,?)"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); //将传过来的数据对应装配到sql语句中 ps.setString(1, message.getMessageTitle()); ps.setString(2, message.getMessageContent()); ps.setInt(3, message.getUserID()); ps.setTimestamp(4, new Timestamp(message.getPublishTime().getTime())); // 获取当前操作的时间 ps.execute(); //将数据提交到数据库中 } catch (Exception e) { e.printStackTrace(); }finally { //关闭相应的数据连接对象 DbConn.close(ps); DbConn.close(conn); } } //分页查询数据库中的记录 @Override public List<Message> findAllMessage(PageBean pageBean) { Connection conn = DbConn.getConnection(); //将消息按发布时间时间进行排序并分页查询出来 String findSQL = "select * from tb_message order by publishTime desc limit ?,?"; PreparedStatement ps = null; ResultSet rs = null; int pageBegin = (pageBean.getCurrPage()-1)*pageBean.getPageSize(); //计算每页相应的查询起点 List<Message> list = new ArrayList<Message>(); try { ps = conn.prepareStatement(findSQL); ps.setInt(1, pageBegin); //设置查询的的起点 ps.setInt(2, pageBean.getPageSize()); //设置查询的每页记录数 rs = ps.executeQuery(); //执行查询返回结果集 while(rs.next()){ //将查询到的数据装配到对象中 Message message = new Message(); message.setMessageID(rs.getInt(1)); message.setMessageTitle(rs.getString(2)); message.setMessageContent(rs.getString(3)); message.setUserID(rs.getInt(4)); message.setPublishTime(rs.getDate(5)); list.add(message); //将数据对象装配进list列表中 } } catch (Exception e) { e.printStackTrace(); }finally { DbConn.close(rs); DbConn.close(ps); DbConn.close(conn); } return list; } //查询总记录数 @Override public int findAllCount() { Connection conn = DbConn.getConnection(); //获得数据库连接 String sql = "select count(*) from tb_message"; int count = 0; //定义一个计数变量 PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); if(rs.next()){ count = rs.getInt(1); //将查询出来的记录数赋给count } } catch (Exception e) { e.printStackTrace(); }finally { DbConn.close(rs); DbConn.close(ps); DbConn.close(conn); } return count; } }
在com.congbbs.test中编写方法进行测试:
@org.junit.Test public void testMessage(){ MessageDao messageDao = MessageDaoFactory.getMessageDaoInstance(); int i = messageDao.findAllCount(); System.out.println("总记录数"+i); PageBean pageBean = new PageBean(1, 5, i, i/5);//查询第一页的数据,每页显示五条记录 List<Message> list=messageDao.findAllMessage(pageBean); for (Message message : list) { System.out.println(message.getMessageTitle()); // 打印消息的标题 } }
测试成功后就通过servlet来将查询到的数据转发显示在页面上
前台分页页面关键代码如下:
<div align="center"> <c:if test="${requestScope.pageBean.currPage != 1}"> <a href="GetMessageList?currPage=1">首页</a> | <a href="GetMessageList?currPage=${requestScope.pageBean.currPage-1}">上一页</a> </c:if> <c:if test="${requestScope.pageBean.currPage!=requestScope.pageBean.tatalPage}"> <a href="GetMessageList?currPage=${requestScope.pageBean.currPage+1}">下一页</a> | <a href="GetMessageList?currPage=${requestScope.pageBean.tatalPage}">尾页</a> </c:if> 当前为第${requestScope.pageBean.currPage}页,共${requestScope.pageBean.tatalPage}页 </div>
在com.congbbs.servlet中创建GetMessageList的servlet类
package com.congbbs.servlet; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.congbbs.dao.MessageDao; import com.congbbs.factory.MessageDaoFactory; import com.congbbs.javabean.Message; import com.congbbs.javabean.PageBean; public class GetMessageList extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); int currentPage = 0; //定义当前页 String currentPageStr = request.getParameter("currPage"); //从界面获取当前页 if(currentPageStr == null || "".equals(currentPageStr)){ //没有传值当前页则设置为默认的第一页 currentPage = 1; }else{ currentPage = Integer.parseInt(currentPageStr); //将获得的当前页字串转为int } MessageDao messageDao = MessageDaoFactory.getMessageDaoInstance(); //获得message业务逻辑实例 int totalCount = messageDao.findAllCount(); //查询总记录数 int totalPage = 0; int pageSize = 5; //定义每页显示的记录数 if(totalCount%pageSize == 0){ //总页数的计算方法整除无余数 totalPage = totalCount/pageSize; //直接设为总页数 }else{ //无法整除页数加一 totalPage = totalCount/pageSize + 1; } PageBean pageBean = new PageBean(currentPage, pageSize,totalCount, totalPage); List<Message> messages = messageDao.findAllMessage(pageBean); request.setAttribute("messages", messages); //将查询的数据传到request域中 request.setAttribute("pageBean", pageBean); //将分页信息传到request域中 ServletContext servletContext = getServletContext(); RequestDispatcher dispatcher = servletContext .getRequestDispatcher("/jsp/messageList.jsp"); //转到消息列表显示界面 dispatcher.forward(request, response); } }
到此分页的显示消息列表的功能也完成了
以上是关于bbs小项目整理(消息分页展现)的主要内容,如果未能解决你的问题,请参考以下文章