轉載至:
https://blog.csdn.net/zhshulin/article/details/26447713
分頁是JAVA WEB項目常用的功能,昨天在Spring MVC中實現了簡單的分頁操作和搜索分頁,在此記錄一下。使用的框架為(MyBatis+SpringMVC+Spring)。
首先我們需要一個分頁的工具類:
有了這個工具類后,首先編寫MyBatis的XxxxMapper.xml配置文件中的SQL語句,如下:
此處我們可以看到,2個<select>需要分別傳入3個和1個參數,此時在對應的DAO文件IXxxxDao中編寫接口來編寫對應的方法,方法名和mapper.xml中的id屬性值一致:
/** * 使用注解方式傳入多個參數,用戶產品分頁,通過登錄用戶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頁面返回處理結果。
@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過程。
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]
2.查詢分頁
關於查詢分頁,大致過程完全一樣,只是第三個參數(上面是loginUserId)需要接受用戶輸入的參數,這樣的話我們需要在控制器中接受用戶輸入的這個參數(頁面中的<input>使用GET方式傳參),然后將其加入到SESSION中,即可完成查詢分頁(此處由於“下一頁”這中超鏈接的原因,使用了不同的JSP頁面處理分頁和搜索分頁,暫時沒找到在一個JSP頁面中完成的方法,出現了重復代碼,這里的重復代碼就是輸出內容的那段代碼,可以單獨拿出去,然后用一個<include>標簽加載到需要的JSP頁面就可以了,這樣可以避免代碼重復):
這里給出控制器的代碼作為參考: