模拟SSM页面分页的测试

使用pageHelper

https://github.com/pagehelper/Mybatis-pageHelper

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md

 查看使用方法

 <!--分页插件  -->
     <dependency>
        <groupId>com.github.pagehelper</groupId>
       <artifactId>pagehelper</artifactId>
       <version>5.0.0</version>
</dependency>

package com.test;

import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


import com.github.pagehelper.PageInfo;
import com.ssm.beans.City;
/*
 * * Build a {@link MockMvc} instance using the given, fully initialized
* (i.e., <em>refreshed</em>) {@link WebApplicationContext}.
* <p>The {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
* will use the context to discover Spring MVC infrastructure and application
* controllers in it. The context must have been configured with a
* {@link javax.servlet.ServletContext ServletContext}.
* 所以@ContextConfiguration(locations={"classpath:resources/spring-*.xml"}) 
* 必须包含org.springframework.web.servlet.DispatcherServlet DispatcherServlet
     * 此处来源与源码中的注释
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration//有了这个就可以将web容器注入
@ContextConfiguration(locations={"classpath:resources/spring-*.xml"})
public class TestSpringMockMvc {
  //传入springmvc的IOC,此处的来源可以查看源码或者看上面复制过来的
  @Autowired
       WebApplicationContext context;
       //虚拟mvc请求,获取处理结果
  MockMvc mvc;
  //初始化
  @Before
  public void initMockMvc(){
  mvc=MockMvcBuilders.webAppContextSetup(context).build();
  }
  @Test
  public void testPage() throws Exception{
 MvcResult result = mvc.perform(MockMvcRequestBuilders.
 get("/countCity/city.do").param("pn", "2"))
 .andReturn();
 MockHttpServletRequest request = result.getRequest();
 PageInfo pageInfo = (PageInfo) request.getAttribute("pageInfo");
 System.out.println("当前页码:"+pageInfo.getPageNum());
 System.out.println("总页码:"+pageInfo.getPages());
 System.out.println("总记录数:"+pageInfo.getTotal());
 System.out.println("页面连续显示的页码数:");获取所有导航页号:getNavigatepageNums
 int[] navigatepageNums = pageInfo.getNavigatepageNums();
 System.out.println("长度是:"+navigatepageNums.length);
 for (int i : navigatepageNums) {
System.out.print(" "+i);
 }
 System.out.println();
 /*
  * page中包装了关于城市的所有信息,查询
  */
 List<City> cityList = pageInfo.getList();
     for (City city : cityList) {
System.out.println(city.getCityName());
System.out.println(city.getCid());
System.out.println(city.getCityName());
System.out.println(city.getCityName());
System.out.println(city.getCityName());
}
  }

}

//jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>城市列表</title>
    <!--引入jquery  -->
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery-1.8.3.js"></script>
    <!-- 引入bootStrap ,不要用不以斜杠开头的路径,那样的话只能保证一次css样式,之后就没有了-->
    <link href="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
  
  </head>
  <body>
     <div class="container">
        <div class="row">
           <h2>SSM-CURD</h2>
        </div>
        <div class="row">
           <div class="col-md-12 col-md-offset-8">
              <button class="btn btn-info">新增</button>
              <button class="btn btn-danger">删除</button>
           </div>
        
        </div>
        <div class="row">
            <div class="col-md-12">
               <table class="table table-hover ">
                  <tr>
                     <th>id</th>
                     <th>city_name</th>
                     <th>pid</th>
                     <th>操作</th>
                  </tr>
                  <!--pageInfo.list是包含city信息的数据,不包含和页码有关的,和分页有关的还在pageInfo中存放着  -->
                  <c:forEach items="${pageInfo.list}" var="info">
                      <tr>
                    <td>${info.cid}</td>
                    <td>${info.cityName }</td>
                    <td>${info.pId }</td>
                    <td>
                       <button class="btn btn-info btn btn-primary btn-sm">
                           <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                                                                                  编辑</button>
                       <button class="btn btn-danger btn btn-primary btn-sm">
                           <span class="glyphicon glyphicon-folder-close" aria-hidden="true"></span>
                                                                                 删除</button>
                    </td>
                      </tr>
                  </c:forEach>
               </table>
            </div>
        </div>
        <div class="row">
           <div class="col-md-12">
              <h5>当前为  ${pageInfo.pageNum}  页,  总页码  ${pageInfo.pages} 页,  共有${pageInfo.total}条记录</h5>
           </div>
        </div>
        <div class="row">
           <div class="col-md-6">
           </div>
           <div class="col-md-6">
              
               <nav aria-label="Page navigation">
 <ul class="pagination">
   <li><a href="${pageContext.request.contextPath}/countCity/city.do?pn=1">首页</a></li>
   <c:if test="${pageInfo.pageNum !=1 }">
                      <li>
     <a href="${pageContext.request.contextPath}/countCity/city.do?pn=${pageInfo.pageNum-1}" aria-label="Previous">
       <span aria-hidden="true">&laquo;</span>
     </a>
     </li>
   </c:if>
   <c:forEach items="${pageInfo.navigatepageNums}" var="pageNum">
        <c:if test="${pageNum==pageInfo.pageNum }">
           <li class="active"><a href="#">${pageNum}</a></li>
        </c:if>
        <c:if test="${pageNum !=pageInfo.pageNum }">
            <li><a href="${pageContext.request.contextPath}/countCity/city.do?pn=${pageNum}">${pageNum}</a></li>
        </c:if>
   </c:forEach>
   <c:if test="${pageInfo.hasNextPage }">
       <li><a href="${pageContext.request.contextPath}/countCity/city.do?pn=${pageInfo.pageNum+1}" aria-label="Next">
       <span aria-hidden="true">&raquo;</span>
       </a>
   </li>
   </c:if>
   <li><a href="${pageContext.request.contextPath}/countCity/city.do?pn=${pageInfo.pages}">末页</a></li>
 </ul>
</nav>
           </div>
        </div>
     </div>
  </body>
</html>


但是这种方法不建议使用,建议使用Ajax 异步

controller

package com.ssm.handlers;

import java.util.List;


import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.beans.City;
import com.ssm.service.CityService;
@Controller
@RequestMapping("/countCity")
public class CityController {
@Autowired
@Qualifier("cityService")
private CityService cityService;
/*
* 实现分页查询,利用插件pageHelper
*/
    @RequestMapping("/city.do")//页面传来的页数默认为1
public String getCity(HttpServletRequest request,
@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
    /*
    * Model 类似于int 用于存储数据(较为复杂的)
    * 1、引入pageHelper分页插件
    * 在查询之前调用,传入页码以及每页的大小
    * startPage(int pageNum, int pageSize)
    */
    PageHelper.startPage(pn, 2);
    //紧跟着的这个查询就是一个分页查询
    List<City> cityList = cityService.getAllCity();
        /*
         * 使用pageInfo包装查询后的结果,只需要将pageInfo交给页面就行了
         * pageInfo封装了分页信息,包括我们查询出来的数据,传入连续显示的页数
         * @param list          page结果
    * @param navigatePages 页码数量,就是最底下可以显示的页数
    * public PageInfo(List<T> list, int navigatePages) {
         */
    PageInfo page=new PageInfo(cityList,2);
    //放进model中可以在页面获取到
    model.addAttribute("pageInfo",page);
    request.setAttribute("cityList", cityList);
        return "/list.jsp";
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值