【java-web】 jsp页面中实现分页效果

实现思路 本人使用的oracle数据库

首先 Dao中的sql语句

SELECT * FROM

(SELECT ROWNUM AS RID ,C.* FROM

CommentInfo C

ORDER BY comment_id DESC)

 WHERE RID BETWEEN 1 AND 5;

解释:ROWNUM  为数据库中结果集的标示 每一个结果集他都有一个不会断续的行标示

RID 为别名 为rowNum起别名

comment_id DESC 降序排列

方法的代码Dao类如下

public List<CommentInfo> getCommentInfoList(int pageIndex,int pageSize) {
  // TODO Auto-generated method stub
  List<CommentInfo> commentInfoList = new ArrayList<CommentInfo>();
  try {
   // 打开连接
   super.openConnection();
   // 创建StringBuffer对象拼接字符串
   StringBuffer sb = new StringBuffer();
   sb.append("SELECT * FROM ");
   sb.append(" (SELECT ROWNUM AS RID ,C.* FROM CommentInfo C ORDER BY comment_id DESC) ");
   sb.append("  WHERE RID BETWEEN ? AND ?  ");
   String sql = sb.toString();
   // 创建执行sql语句命令对象
   super.preparedStatement = super.connection.prepareStatement(sql);
   //设置分页的参数
   super.preparedStatement.setInt(1, (pageIndex-1)*pageSize+1);
   super.preparedStatement.setInt(2, pageIndex*pageSize);
   
   super.resultSet = super.preparedStatement.executeQuery();
   while (super.resultSet.next()) {
    CommentInfo commentInfos = new CommentInfo();
    commentInfos.setCommentId(super.resultSet.getLong("comment_id"));// 留言编号
    commentInfos.setCommentTitle(super.resultSet.getString("comment_title"));// 留言标题
    commentInfos.setCommentReply(super.resultSet.getString("comment_reply"));// 留言回复
    commentInfos.setCommentContent(super.resultSet.getString("comment_content"));// 发表留言的内容
    commentInfos.setCommentCreateTime(super.resultSet.getTimestamp("comment_createTime"));// 创建时间
    commentInfos.setCommentReplyTime(super.resultSet.getTimestamp("comment_replyTime"));// 回复时间
    commentInfos.setUserId(super.resultSet.getString("user_id"));// 用户编号
    commentInfoList.add(commentInfos);
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   // 关闭结果集
   super.closeConnection();
  }
  return commentInfoList;
 }

在servlet中代码如下

//设置编码格式
  request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8");
  
  //定义页面
  int pageIndex = 1;
  //定义每页显示的数量
  int pageSize = 3;
  //定义总行数
  int rowCount = 0;
  //定义中页数
  int pageCount = 0;

//判断页面是否回传参数
  if(request.getParameter("pageIndex")!=null){
   pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
  }
  //创建评论集合对象
  List<CommentInfo> commentInfoList = new ArrayList<CommentInfo>();
  //创建评论Biz对象
  CommentInfoBiz commentInfoBizImpl = new CommentInfoBizImpl();
  //获得总行数
  rowCount = commentInfoBizImpl.getCommentInfoCount();
  
  //计算总页数
  pageCount = (rowCount%pageSize==0)?(rowCount/pageSize):(rowCount/pageSize+1);
  
  //调用根据页码查询的方法
  commentInfoList = commentInfoBizImpl.getCommentInfoList(pageIndex,pageSize);
  
  //将数据保存到request中
  request.setAttribute("pageIndex", pageIndex);
  request.setAttribute("pageCount", pageCount);
  request.setAttribute("commentInfoList", commentInfoList);
  
  //请求转发
  request.getRequestDispatcher("guestBook.jsp").forward(request, response);

jsp页面中的代码

<ul class="clearfix">
     <%--判断当前是否为第一页 如果为第一页则按钮不可用--%>
     <li><a <j:if test="${requestScope.pageIndex!=1}">href="DoGuestBook?pageIndex=1"</j:if>>首页</a></li>
     <li><a <j:if test="${requestScope.pageIndex!=1}">href="DoGuestBook?pageIndex=${requestScope.pageIndex-1}"</j:if>>上一页</a></li>
     <%--使用循环显示当前的页码 --%>
     <j:forEach var="index" begin="${requestScope.pageIndex }" end="${requestScope.pageIndex+4 }">
      <%--判断当前的页码是否已经大于当前的总页码数 如果大于当前的总页码数则不在累加显示页码--%>
      <j:if test="${index<=requestScope.pageCount}">
       <!-- 判断是否为当前页码如果是当前页码的时候改变其样式 -->
       <j:if test="${index==requestScope.pageIndex}">
        <li class="current">${index }</li>
       </j:if>
       <j:if test="${index!=requestScope.pageIndex}">
        <li><a href="DoGuestBook?pageIndex=${index }">${index }</a></li>
       </j:if>
      </j:if>
     </j:forEach>  
     <%--判断是否为最后一页如果未最后一页则按钮不可用 --%>   
     <li><a <j:if test="${requestScope.pageIndex!=requestScope.pageCount}">href="DoGuestBook?pageIndex=${requestScope.pageIndex+1}"</j:if>>下一页</a></li>
     <li><a <j:if test="${requestScope.pageIndex!=requestScope.pageCount}">href="DoGuestBook?pageIndex=${requestScope.pageCount}"</j:if>>尾页</a></li>
    </ul>

本人的想法  希望对您有所帮助!如果您有更好的想法和思路请您于本人分享一哈!谢谢!!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值