如何进行模糊分页
使用模糊分頁(yè)需要3個(gè)參數(shù):關(guān)鍵字:key,當(dāng)前頁(yè):page,查詢(xún)內(nèi)容個(gè)數(shù):count
核心代碼:
//所有菜單列表+分頁(yè)private void foodList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String page = request.getParameter("page");//模糊查詢(xún) 關(guān)鍵字String key = request.getParameter("foodName");if(null==page) {page="";}if(null==key) {key="";}Integer count = menuService.count(key);PageUtils pageUtils = new PageUtils(page,6,count);//查找所有菜品并分頁(yè)List<food> list = menuService.foodList(key,(pageUtils.getCurrPage()-1)*6,6);request.setAttribute("pageUtils", pageUtils);request.setAttribute("list", list);request.setAttribute("key", key);request.getRequestDispatcher("/front/detail/caidan.jsp").forward(request, response);}Html頁(yè)面:
<a href="<%=basePath%>/menu?method=foodList&page=1&foodName=${key}">首頁(yè)</a> <a href="<%=basePath%>/menu?method=foodList&page=${pageUtils.prevPage}&foodName=${key}">上一頁(yè)</a> <a href="<%=basePath%>/menu?method=foodList&page=${pageUtils.nextPage}&foodName=${key}">下一頁(yè)</a> <a href="<%=basePath%>/menu?method=foodList&page=${pageUtils. lastPage}&foodName=${key}">尾頁(yè)</a>附件:pageUtils代碼:
package com.baidu.utils;public class PageUtils {private String page; //當(dāng)前頁(yè)private int pageSize; //每頁(yè)顯示數(shù)目private int count; //總數(shù)據(jù)個(gè)數(shù)public PageUtils(String page, int pageSize, int count) {super();this.page = page;this.pageSize = pageSize;this.count = count;//初始化當(dāng)前頁(yè)initCurrPage();//初始化最后一頁(yè)initLastPage() ;//初始化下一頁(yè)initNextPage() ;//初始化上一頁(yè)initPrevPage();}private int currPage;private int nextPage;private int lastPage;private int prevPage;public int getNextPage() {return nextPage;}public int getLastPage() {return lastPage;}public int getPrevPage() {return prevPage;}public int getPageSize() {return pageSize;}public int getCurrPage() {return currPage;}private void initCurrPage() {if(null ==page|| "".equals(page)) {page="1";}currPage=Integer.parseInt(page);}private void initLastPage() {lastPage=count/pageSize;if(count%pageSize !=0) {lastPage++;}}private void initNextPage() {nextPage=currPage==lastPage?lastPage:currPage+1;}private void initPrevPage() {prevPage=currPage==1?currPage:currPage-1;}}?
總結(jié)
- 上一篇: mysql判断不等于空的脚本_Shell
- 下一篇: rocketmq 消息 自定义_跟我学R