轉載至:html
https://blog.csdn.net/zhshulin/article/details/26447713前端
分頁是JAVA WEB項目經常使用的功能,昨天在Spring MVC中實現了簡單的分頁操做和搜索分頁,在此記錄一下。使用的框架爲(MyBatis+SpringMVC+Spring)。java
首先咱們須要一個分頁的工具類:瀏覽器
有了這個工具類後,首先編寫MyBatis的XxxxMapper.xml配置文件中的SQL語句,以下:session
此處咱們能夠看到,2個<select>須要分別傳入3個和1個參數,此時在對應的DAO文件IXxxxDao中編寫接口來編寫對應的方法,方法名和mapper.xml中的id屬性值一致:app
/** * 使用註解方式傳入多個參數,用戶產品分頁,經過登陸用戶ID查詢 * @param page * @param userId * @return startPos},#{pageSize} */ public List<Products> selectProductsByPage(@Param(value="startPos") Integer startPos,@Param(value="pageSize") Integer pageSize,@Param(value="userId") Integer userId); /** * 取得產品數量信息,經過登陸用戶ID查詢 * @param userId * @return */ public long getProductsCount(@Param(value="userId") Integer userId);
接口定義完成以後須要編寫相應的業務接口和實現方法,在接口中定義這樣一個方法,而後實現類中覆寫一下:框架
/** * 分頁顯示商品 * @param request * @param model * @param loginUserId */ void showProductsByPage(HttpServletRequest request,Model model,int loginUserId);
接下來實現類中的方法就是要調用DAO層和接受Controller傳入的參數,進行業務邏輯的處理,request用來獲取前端傳入的參數,model用來向JSP頁面返回處理結果。jsp
@Override public void showProductsByPage(HttpServletRequest request, Model model,int loginUserId) { String pageNow = request.getParameter("pageNow"); Page page = null; List<ProductWithBLOBs> products = new ArrayList<ProductWithBLOBs>(); int totalCount = (int) productDao.getProductsCount(loginUserId); if (pageNow != null) { page = new Page(totalCount, Integer.parseInt(pageNow)); allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId); } else { page = new Page(totalCount, 1); allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId); } model.addAttribute("products", products); model.addAttribute("page", page); }
接下來是控制器的編寫,當用戶須要跳轉到這個現實產品的頁面時,就須要通過這個控制器中相應方法的處理,這個處理過程就是調用業務層的方法來完成,而後返回結果到JSP動態顯示,服務器端生成好頁面後傳給客戶端(瀏覽器)現實,這就是一個MVC過程。ide
JSP頁面接受部分,每一個人都同樣,也就是結合JSTL和EL來寫,(在循環輸出的時候也作了判斷,若是接受的參數爲空,那麼輸出暫無商品,只有接受的參數不爲空的時候,才循環輸出,使用<<c:when test="${}">結合<c:otherwise>),這裏只給出分頁的相關代碼:
分頁接收參數代碼:
轉載至:https://blog.csdn.net/zhshulin/article/details/26447713
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!--引入JSTL核心標記庫的taglib指令--> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>顯示留言</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <a href="success.jsp">返回</a> <table border="1"> <tr> <th width="150">留言數</th> <th width="150">主題</th> <th width="150">內容</th> <th width="150">留言時間</th> <th width="150">留言人</th> <th width="150">刪除選項</th> </tr> <c:forEach items="${requestScope.messages}" var="message"> <tr> <td width="100">${message.messageId}</td> <td width="100">${message.title}</td> <td width="500">${message.content}</td> <td width="200">${message.time}</td> <td width="100">${message.userName}</td> <td width="100"> <form action="MessageServlet?status=deleteMessage" method="post"> <input type="hidden" value="${message.messageId}" name="messageId"> <input type="submit" value="刪除" onclick="return confirm('肯定刪除嗎?')"> </form></td> </tr> </c:forEach> </table> <center> <div> 第${requestScope.currentPage}頁/共${requestScope.countPage}頁 <a href="${pageContext.request.contextPath}/MessageServlet?status=getMessage¤ttPage=1">首頁</a><span> </span> <c:choose> <c:when test="${requestScope.currentPage==1}"> 上一頁 </c:when> <c:otherwise> <a href="${pageContext.request.contextPath}/MessageServlet?status=getMessage<span style="font-family: Arial, Helvetica, sans-serif;">¤ttPage</span>=${requestScope.currentPage-1}">上一頁</a> </c:otherwise> </c:choose> <%--計算begin和end --%> <c:choose> <%--若是總頁數不足10,那麼就把全部的頁都顯示出來 --%> <c:when test="${requestScope.countPage<=10}"> <c:set var="begin" value="1" /> <c:set var="end" value="${requestScope.countPage}" /> </c:when> <c:otherwise> <%--若是總頁數大於10,經過公式計算出begin和end --%> <c:set var="begin" value="${requestScope.currentPage-5}" /> <c:set var="end" value="${requestScope.currentPage+4}" /> <%--頭溢出 --%> <c:if test="${begin<1}"> <c:set var="begin" value="1"></c:set> <c:set var="end" value="10"></c:set> </c:if> <%--尾溢出 --%> <c:if test="${end>requestScope.countPage}"> <c:set var="begin" value="${requestScope.countPage - 9}"></c:set> <c:set var="end" value="${requestScope.countPage}"></c:set> </c:if> </c:otherwise> </c:choose> <%--循環顯示頁碼列表 --%> <c:forEach var="i" begin="${begin}" end="${end}"> <c:choose> <c:when test="${i == requestScope.currentPage}"> [${i}] </c:when> <c:otherwise> <a href="<c:url value ='/MessageServlet?status=getMessage ¤tPage=${i}'/>">[${i}]</a> </c:otherwise> </c:choose> </c:forEach> <c:choose> <c:when test="${requestScope.currentPage==requestScope.countPage}"> 下一頁 </c:when> <c:otherwise> <a href="${pageContext.request.contextPath}/MessageServlet?status=getMessage¤tPage=${requestScope.currentPage+1}"> 下一頁</a> </c:otherwise> </c:choose> <span> </span><a href="${pageContext.request.contextPath}/MessageServlet?status=getMessage¤tPage=${requestScope.countPage}">尾頁</a> </div> </center> </body> </html>
[html]
關於查詢分頁,大體過程徹底同樣,只是第三個參數(上面是loginUserId)須要接受用戶輸入的參數,這樣的話咱們須要在控制器中接受用戶輸入的這個參數(頁面中的<input>使用GET方式傳參),而後將其加入到SESSION中,便可完成查詢分頁(此處因爲「下一頁」這中超連接的緣由,使用了不一樣的JSP頁面處理分頁和搜索分頁,暫時沒找到在一個JSP頁面中完成的方法,出現了重複代碼,這裏的重複代碼就是輸出內容的那段代碼,能夠單獨拿出去,而後用一個<include>標籤加載到須要的JSP頁面就能夠了,這樣能夠避免代碼重複):
這裏給出控制器的代碼做爲參考: