(转)ssm框架分页实现后端
转载至:
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页面就可以了,这样可以避免代码重复):
这里给出控制器的代码作为参考: