java web 開發 分頁功能 mysql資料庫

來源:互聯網
上載者:User

list.jsp:

<div class="right_content">
  <h2 class="bulletin_h2">系統公告</h2>
  <div class="bulletin_content">
   <ul>
    <c:forEach items="${notices}" var="notice">
     <li><em>>></em> <a href="getnecontent?id=${notice.id}">${notice.title}</a>
      <span>刪除</span>
      <span>編輯</span>
      <span>${notice.pubtime }</span>
     </li>
    </c:forEach>
   </ul>
    <c:if test="${currentPage != 1}">
     <a href="noticelist?page=${currentPage - 1}">Previous</a>
    </c:if>
    <c:forEach begin="1" end="${noOfPages}" var="i">
     <c:choose>
      <c:when test="${currentPage eq i}">
       <td>${i}</td>
      </c:when>
      <c:otherwise>
       <td><a href="noticelist?page=${i}">${i}</a>
       </td>
      </c:otherwise>
     </c:choose>
    </c:forEach>
    <c:if test="${currentPage lt noOfPages}">
     <td><a href="noticelist?page=${currentPage + 1}">Next</a>
     </td>
    </c:if>
  </div>
 </div>

list.java:

public ArrayList<Notice> getNotices(int offset,int recordsPerPage){
  ResultSet rs=null;
  PreparedStatement ps =null;
  ArrayList<Notice> notices = new ArrayList<Notice>();
  Connection conn = JdbcUtils.getConnection();
  String sql = "select SQL_CALC_FOUND_ROWS id,title,content,person,filepath,pubtime from notice order by id desc limit ?,? ";
  try {
   ps = conn.prepareStatement(sql);
   ps.setInt(1, offset);
   ps.setInt(2, recordsPerPage);
   rs = ps.executeQuery();
   while(rs.next()){
    Notice notice = new Notice();
    notice.setId(rs.getInt("id"));
    notice.setTitle(rs.getString("title"));
    notice.setContent(rs.getString("content"));
    notice.setPerson(rs.getString("person"));
    notice.setFilepath(rs.getString("filepath"));
    notice.setPubtime(rs.getString("pubtime"));
    notices.add(notice);
   }
   rs.close();
   rs = ps.executeQuery("SELECT FOUND_ROWS()");
   if(rs.next())
               this.noOfRecords = rs.getInt(1);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return notices;
 }

servlet:

  //處理亂碼
  request.setCharacterEncoding("UTF-8");
  response.setContentType("text/html; charset=gbk"); 
  
  int page = 1;
        int recordsPerPage = 10;
        if(request.getParameter("page") != null)
            page = Integer.parseInt(request.getParameter("page"));
       
  NoticeDAO nd = new NoticeDAOImpl();
  ArrayList<Notice> notices = nd.getNotices((page-1)*recordsPerPage,recordsPerPage);
  int noOfRecords = nd.getNoOfRecords();
  int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
  
  request.setAttribute("notices", notices);
  request.setAttribute("noOfPages", noOfPages);
  request.setAttribute("currentPage", page);
  request.getRequestDispatcher("noticelist.jsp").forward(request, response);

參考資料:http://www.cnblogs.com/super119/archive/2011/01/13/1934997.html

QQ群:127402101,進群請修改備忘。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.