分頁(yè)是JAVA WEB項(xiàng)目常用的功能,昨天在Spring MVC中實(shí)現(xiàn)了簡(jiǎn)單的分頁(yè)操作和搜索分頁(yè),,在此記錄一下,。使用的框架為(MyBatis+SpringMVC+Spring),。
首先我們需要一個(gè)分頁(yè)的工具類: 1.分頁(yè)
有了這個(gè)工具類后,,首先編寫MyBatis的XxxxMapper.xml配置文件中的SQL語(yǔ)句,,如下:-
- <select id="selectProductsByPage" resultMap="返回值類型">
- select
- *
- from 表名 WHERE user_id = #{userId,jdbcType=INTEGER} limit #{startPos},#{pageSize}
- </select>
-
- <select id="getProductsCount" resultType="long">
- SELECT COUNT(*) FROM 表名 WHERE user_id = #{userId,jdbcType=INTEGER}
- </select>
此處我們可以看到,2個(gè)<select>需要分別傳入3個(gè)和1個(gè)參數(shù),,此時(shí)在對(duì)應(yīng)的DAO文件IXxxxDao中編寫接口來編寫對(duì)應(yīng)的方法,,方法名和mapper.xml中的id屬性值一致:
-
-
-
-
-
-
- public List<Products> selectProductsByPage(@Param(value="startPos") Integer startPos,@Param(value="pageSize") Integer pageSize,@Param(value="userId") Integer userId);
-
-
-
-
-
-
- public long getProductsCount(@Param(value="userId") Integer userId);
接口定義完成之后需要編寫相應(yīng)的業(yè)務(wù)接口和實(shí)現(xiàn)方法,在接口中定義這樣一個(gè)方法,,然后實(shí)現(xiàn)類中覆寫一下:
-
-
-
-
-
-
- void showProductsByPage(HttpServletRequest request,Model model,int loginUserId);
接下來實(shí)現(xiàn)類中的方法就是要調(diào)用DAO層和接受Controller傳入的參數(shù),,進(jìn)行業(yè)務(wù)邏輯的處理,request用來獲取前端傳入的參數(shù),,model用來向JSP頁(yè)面返回處理結(jié)果,。 - @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);
- }
接下來是控制器的編寫,當(dāng)用戶需要跳轉(zhuǎn)到這個(gè)現(xiàn)實(shí)產(chǎn)品的頁(yè)面時(shí),,就需要經(jīng)過這個(gè)控制器中相應(yīng)方法的處理,,這個(gè)處理過程就是調(diào)用業(yè)務(wù)層的方法來完成,然后返回結(jié)果到JSP動(dòng)態(tài)顯示,,服務(wù)器端生成好頁(yè)面后傳給客戶端(瀏覽器)現(xiàn)實(shí),,這就是一個(gè)MVC過程。 -
-
-
-
-
-
-
- @RequestMapping(value = "映射路徑", method = RequestMethod.GET)
- public String showMyProduct(HttpServletRequest request, Model model) {
-
- User loginUser = (User) request.getSession().getAttribute("loginUser");
-
- if (loginUser == null || "".equals(loginUser)) {
- return "redirect:/";
- }
-
- int loginUserId = loginUser.getUserId();
-
- this.productService.showProductsByPage(request, model, loginUserId);
-
- return "跳轉(zhuǎn)到的JSP路徑";
- }
JSP頁(yè)面接受部分我就不寫了,,每個(gè)人都一樣,,也就是結(jié)合JSTL和EL來寫,(在循環(huán)輸出的時(shí)候也做了判斷,,如果接受的參數(shù)為空,,那么輸出暫無商品,,只有接受的參數(shù)不為空的時(shí)候,才循環(huán)輸出,,使用<<c:when test="${}">結(jié)合<c:otherwise>),,這里只給出分頁(yè)的相關(guān)代碼:
-
- <div align="center">
- <font size="2">共 ${page.totalPageCount} 頁(yè)</font> <font size="2">第
- ${page.pageNow} 頁(yè)</font> <a href="myProductPage?pageNow=1">首頁(yè)</a>
- <c:choose>
- <c:when test="${page.pageNow - 1 > 0}">
- <a href="myProductPage?pageNow=${page.pageNow - 1}">上一頁(yè)</a>
- </c:when>
- <c:when test="${page.pageNow - 1 <= 0}">
- <a href="myProductPage?pageNow=1">上一頁(yè)</a>
- </c:when>
- </c:choose>
- <c:choose>
- <c:when test="${page.totalPageCount==0}">
- <a href="myProductPage?pageNow=${page.pageNow}">下一頁(yè)</a>
- </c:when>
- <c:when test="${page.pageNow + 1 < page.totalPageCount}">
- <a href="myProductPage?pageNow=${page.pageNow + 1}">下一頁(yè)</a>
- </c:when>
- <c:when test="${page.pageNow + 1 >= page.totalPageCount}">
- <a href="myProductPage?pageNow=${page.totalPageCount}">下一頁(yè)</a>
- </c:when>
- </c:choose>
- <c:choose>
- <c:when test="${page.totalPageCount==0}">
- <a href="myProductPage?pageNow=${page.pageNow}">尾頁(yè)</a>
- </c:when>
- <c:otherwise>
- <a href="myProductPage?pageNow=${page.totalPageCount}">尾頁(yè)</a>
- </c:otherwise>
- </c:choose>
- </div>
-
2.查詢分頁(yè) 關(guān)于查詢分頁(yè),大致過程完全一樣,,只是第三個(gè)參數(shù)(上面是loginUserId)需要接受用戶輸入的參數(shù),,這樣的話我們需要在控制器中接受用戶輸入的這個(gè)參數(shù)(頁(yè)面中的<input>使用GET方式傳參),然后將其加入到SESSION中,,即可完成查詢分頁(yè)(此處由于“下一頁(yè)”這中超鏈接的原因,,使用了不同的JSP頁(yè)面處理分頁(yè)和搜索分頁(yè),暫時(shí)沒找到在一個(gè)JSP頁(yè)面中完成的方法,,出現(xiàn)了重復(fù)代碼,,這里的重復(fù)代碼就是輸出內(nèi)容的那段代碼,可以單獨(dú)拿出去,,然后用一個(gè)<include>標(biāo)簽加載到需要的JSP頁(yè)面就可以了,,這樣可以避免代碼重復(fù)): 這里給出控制器的代碼作為參考:
-
-
-
-
-
-
- @RequestMapping(value = "映射地址", method = RequestMethod.GET)
- public String searchForProducts(HttpServletRequest request, Model model) {
- HttpSession session = request.getSession();
-
- String param = request.getParameter("param");
-
- String condition = (String) session.getAttribute("condition");
-
-
- if (condition == null) {
- condition = new String();
- session.setAttribute("condition", condition);
-
- if (param == null || "".equals(param)) {
- return "private/space/ProductSearchResult";
- }
- }
-
- if (param != null && !("".equals(param))) {
- condition = param;
- session.setAttribute("condition", condition);
- }
-
- this.productService.showSearchedProductsByPage(request, model, condition);
-
- return "跳轉(zhuǎn)的頁(yè)面";
- }
|