转载至: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页面就能够了,这样能够避免代码重复):
这里给出控制器的代码做为参考: