正在回答
同学你好,每页6条留言,所以应该判断count查询到的总记录数与6的余数。
参考代码如下:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
servlet:
private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int page=0;
String currPage=request.getParameter("page");
if(currPage==null) {
page=1;
}else {
page=Integer.parseInt(currPage);
}
MessageService messageService=new MessageServiceImpl();
List<Message> list=messageService.findAll();
request.setAttribute("list", list);
//分页查询
PageBean pageBean=messageService.findByPage(page);
System.out.println(pageBean);
request.setAttribute("pageBean", pageBean);
//System.out.println(list);
request.getRequestDispatcher("/message_list.jsp").forward(request, response);
}
serviceImpl:
@Override
public PageBean<Message> findByPage(int page) {
PageBean<Message> pageBean=new PageBean<Message>();
//封装当前页数
pageBean.setPage(page);
//封装每页显示的记录数
int limit=6;
pageBean.setLimit(limit);
//封装总记录数
MessageDao messageDao=new MessageDaoImpl();
int totalCount=messageDao.findCount();
pageBean.setTotalCount(totalCount);
//封装总页数
int totalPage=0;
if(totalCount%limit==0) {
totalPage=totalCount%limit;
}else {
totalPage=totalCount%limit+1;
}
pageBean.setTotalPage(totalPage);
//封装每页所显示数据的集合
int begin=(page-1) * limit;
List<Message> list=messageDao.findByPage(begin,limit);
pageBean.setList(list);;
return pageBean;
}
DaoImpl:
@Override
public int findCount() {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
Long count=0l;
try {
conn=JDBCUtils2.getConnection();
String sql="select count(*) as count from message";
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
if(rs.next()) {
count=rs.getLong("count");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils2.release(pstm, conn, rs);
}
return count.intValue();
}
@Override
public List<Message> findByPage(int begin, int limit) {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
List<Message> list=null;
try {
conn=JDBCUtils2.getConnection();
String sql="select * from message limit ?,?";
pstm=conn.prepareStatement(sql);
pstm.setInt(1, begin);
pstm.setInt(2, limit);
rs=pstm.executeQuery();
list=new ArrayList<Message>();
while(rs.next()) {
Message message=new Message();
message.setId(rs.getInt("id"));
message.setUser_id(rs.getInt("user_id"));
message.setUsername(rs.getString("username"));
message.setTitle(rs.getString("title"));
message.setContent(rs.getString("content"));
message.setCreate_time(rs.getTimestamp("Create_time"));
list.add(message);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils2.release(pstm, conn, rs);
}
return list;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta charset="UTF-8">
<title>留言板</title>
<link rel="stylesheet" href="css/index.css">
<script type="text/javascript">
</script>
</head>
<body>
<header>
<div class="container">
<% if (null != request.getSession().getAttribute("exUser")) {%>
<nav>
<a href="${pageContext.request.contextPath }/MessageServlet?method=findOne">我的留言</a>
</nav>
<nav>
<a href="${pageContext.request.contextPath }/MessageServlet?method=edit2">我的信息</a>
</nav>
<%} else { %>
<nav>
<a href="">登录</a>
<a href="">注册</a>
</nav>
<% } %>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>慕课网留言板</h1>
<p>慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好的互联网技术牛人,也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。 </p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<c:forEach items="${pageBean.list}" var="ml">
<div class="alt-item">
<div class="alt-head">
<div class="alt-info">
<span>作者:${ml.username}<a href=""></a></span>
<span>时间:${ml.create_time}</span>
</div>
</div>
<div class="alt-content">
<h3>${ml.title}</h3>
<p>${ml.content}</p>
</div>
<div style="float:right">
<td>
<a href="${pageContext.request.contextPath }/MessageServlet?method=edit&id=${ml.id}">修改</a>
<a href="${pageContext.request.contextPath }/MessageServlet?method=delete&id=${ml.id}">删除</a>
</td>
</div>
</c:forEach>
</section>
<section class="page">
<div class="container">
<div>
<td>
<a href="${pageContext.request.contextPath }/MessageServlet?method=addPage">点我留言</a>
</td>
</div>
<div id="pagefy">
<ul>
<c:if test="${pageBean.page!=1 }">
<li><a href="${pageContext.request.contextPath }/MessageServlet?method=findAll&page=1">首页</a></li>
<li><a href="${pageContext.request.contextPath }/MessageServlet?method=findAll&page=${pageBean.page-1}">上一页</a></li>
</c:if>
<li><a href="">当前第${pageBean.page}页</a></li>
<c:if test="${pageBean.page!=pageBean.totalPage }">
<li><a href="${pageContext.request.contextPath }/MessageServlet?method=findAll&page=${pageBean.page+1}">下一页</a></li>
<li><a href="${pageContext.request.contextPath }/MessageServlet?method=findAll&page=${pageBean.totalPage}">尾页</a></li>
</c:if>
</ul>
</div>
</div>
</div>
<!-- 分页内容参考视频中老师源码 -->
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
</html>
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星