JavaWeb 簡單分頁的實現(xiàn):
這次主要是講解一下通過登錄后對得到的數(shù)據(jù)進行分頁,首先我們新建一個登錄頁面login.jsp,因為我們主要學(xué)習(xí)一下分頁,所以登錄驗證的部分不再闡述,主要代碼如下:
<form action="pageServlet"> 用戶名:<input type="text" name="username"><br> 密 碼:<input type="text" name="password"><br> <input type="submit" value="提交"> </form>
首先建立實體類User.java并添加get和set方法:
public class User {  private String username;  private String password;  public String getUsername() {    return username;  }  public void setUsername(String username) {    this.username = username;  }  public String getPassword() {    return password;  }  public void setPassword(String password) {    this.password = password;  }  }我們可以看到form表單是提交到pageServlet中,所以我們新建一個PageServlet,并在Servlet中獲取到數(shù)據(jù),同時做一些分頁的準(zhǔn)備,具體含義可以參照注釋理解,PageServlet代碼:
public class PageServlet extends HttpServlet {  public void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {    List<User> list = new ArrayList<User>();    // 在這里我不再連接數(shù)據(jù)庫而是用虛擬的數(shù)據(jù)進行測試效果,小伙伴可以連接數(shù)據(jù)庫查詢到之后返回一個list    for (int i = 1; i < 7; i++) {      User user1 = new User();      user1.setUsername("第" + i + "個用戶名");      user1.setPassword("第" + i + "密碼");      list.add(user1);    }    HttpSession session = request.getSession();    // 將數(shù)據(jù)存到session中以便于在前臺獲取    session.setAttribute("userList", list);    //獲取當(dāng)前頁的頁數(shù)并轉(zhuǎn)為int類型,最終將數(shù)據(jù)存到session中    int pageNos;    if (request.getParameter("pageNos") == null        || Integer.parseInt(request.getParameter("pageNos")) < 1) {      pageNos = 1;    } else {      pageNos = Integer.parseInt(request.getParameter("pageNos"));    }    session.setAttribute("pageNos", pageNos);    // 定義總頁數(shù)并存到session中    int countPage = 3;    // 在實際開發(fā)中我們的總頁數(shù)可以根據(jù)sql語句得到查詢到的總條數(shù),然后用總條數(shù)除每頁的條數(shù)得到總頁數(shù)    session.setAttribute("countPage", countPage);    request.getRequestDispatcher("index.jsp").forward(request, response);  }  public void doPost(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {  }}在上述代碼中我們最終將轉(zhuǎn)發(fā)到index.jsp頁面,此時我們所有的數(shù)據(jù)都將顯示在index.jsp中,用JSTL和EL表達式獲取得到,index.jsp主要代碼如下:
<body>  <c:forEach items="${userList}" var="user" begin="${(pageNos-1)*2 }"    end="${pageNos*2-1}">    <center>      <div>${user.username}</div>    </center>    <center>      <div>${user.password}</div>    </center>  </c:forEach>  <center>  <c:if test="${pageNos>1 }"><a href="pageServlet?pageNos=1" >首頁</a><a href="pageServlet?pageNos=${pageNos-1 }">上一頁</a></c:if><c:if test="${pageNos <countPage }"><a href="pageServlet?pageNos=${pageNos+1 }">下一頁</a><a href="pageServlet?pageNos=${countPage }">末頁</a></c:if></center><form action="pageServlet"><h4 align="center">共${countPage}頁 <input type="text" value="${pageNos}" name="pageNos" size="1">頁<input type="submit" value="go"></h4></form> </body>第二行中我們用<c:forEach >對session.setAttribute();中的內(nèi)容進行獲取。注意,這里我默認是每頁兩條數(shù)據(jù),所以是(pageNos-1)*2,如果每頁N條數(shù)據(jù)則需將2改為N,當(dāng)然N也可以從后臺Servlet中獲取得到。
同時,因為我們在index.jsp中用了JSTL表達式,所以記得要導(dǎo)入引用:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
到這里我們就完成了一個簡單的分頁,快去試試吧。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答
圖片精選