分页

pom.xml::

<!-- mybatis-pageHelper -->
<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>5.0.0</version>
</dependency>

mybatis-config.xml:

<!-- 分页插件 -->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <property name="reasonable" value="true"/>
    </plugin>
</plugins>
cintroller:

@RequestMapping("/emps")
public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
   //分页查询
   int pageSize=5;
   //传入页码,以及每页的大小
   PageHelper.startPage(pn, pageSize);
   List<Employee> emps=employeeService.getAll();
   //PageInfo封装查询后结果,传入连续显示的页数
   PageInfo page=new PageInfo(emps,5);
   model.addAttribute("pageInfo",page);
   return "list";
}

testMVC:

/*
 * spring4的测试需要servlet-api 3.0以上版本
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:applicationContext.xml",
      "file:D:/cmbc/codeAtNight/crud/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})
public class MVCTest {
   //传入springMVC的ioc
   @Autowired
   WebApplicationContext context;
   MockMvc mockMvc; //虚拟springmvc请求,获取到处理结果
   
   @Before
   public void initMockMvc(){
      mockMvc=MockMvcBuilders.webAppContextSetup(context).build();
   }
   @Test
   public void testPage() throws Exception{
      //模拟请求拿到返回值
      MvcResult result=mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();
      
      //请求成功以后,请求域中会有PageInfo,我们可以取出PageInfo进行验证
      MockHttpServletRequest request=result.getRequest();
      PageInfo pi=(PageInfo) request.getAttribute("pageInfo");
      //当前页码
      System.out.println(pi.getPageNum());
      //总页码
      System.out.println(pi.getPages());
      //每页的个数
      System.out.println(pi.getSize());
      //总数量
      System.out.println(pi.getTotal());
      //连续显示的页码
      int[] nums=pi.getNavigatepageNums();
      for(int i:nums){System.out.print(i+",");}
      List<Employee> list=pi.getList();
      for(Employee employee:list){
         System.out.println("ID:"+employee.getEmpId()+"==>Name:"+employee.getEmpName());
      }
   }
}

jsp:

<c:forEach items="${pageInfo.list}" var="item">
                    <tr>
                        <th>${item.id}</th>
                        <th>${item.title}</th>
                        <th>${item.h5url}</th>
                        <th>${item.need_login}</th>
                        <th>${item.code}</th>
                        <th>${item.start_time}</th>
                        <th>${item.end_time}</th>
                        <th>
                            <button class="btn btn-primary btn-sm" οnclick="updateHints('${item.id}','${item.title}','${item.h5url}','${item.need_login}','${item.code}','${item.start_time}','${item.end_time}')">
                                <span class="glyphicon glyphicon-pencil" aria-hidden="true" ></span>
                                编辑
                            </button>
                            <button class="btn btn-danger btn-sm" οnclick="deleteHints('${item.id}')">
                                <span class="glyphicon glyphicon-trash" aria-hidden="true" ></span>
                                删除
                            </button>
                        </th>

                    </tr>
                </c:forEach>

<%--显示分页信息--%>
        <div class="row">
            <%--分页文字信息--%>
            <div class="col-md-6">
                当前${pageInfo.pageNum}页,总${pageInfo.pages}页,总${pageInfo.total}条记录
            </div>
            <%--分页条信息--%>
            <div class="col-md-6">
                <nav aria-label="Page navigation">
                    <ul class="pagination">
                        <li><a href="${APP_PATH}fenyeHints?pn=1">首页</a></li>
                        <c:if test="${pageInfo.hasPreviousPage}">
                            <li>
                                <a href="${APP_PATH}/fenyeHints?pn=${pageInfo.pageNum-1}" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                        </c:if>
                        <c:forEach items="${pageInfo.navigatepageNums}" var="page_Num">
                            <c:if test="${page_Num==pageInfo.pageNum}">
                                <li class="active"><a href="#">${page_Num}</a></li>
                            </c:if>
                            <c:if test="${page_Num!=pageInfo.pageNum}">
                                <li ><a href="${APP_PATH}/fenyeHints?pn=${page_Num}">${page_Num}</a></li>
                            </c:if>
                        </c:forEach>
                     <%--   <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">5</a></li>--%>
                        <c:if test="${pageInfo.hasNextPage}">
                            <li>
                                <a href="${APP_PATH}/fenyeHints?pn=${pageInfo.pageNum+1}" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </c:if>
                        <li><a href="${APP_PATH}/fenyeHints?pn=${pageInfo.pages}">末页</a></li>
                    </ul>
                </nav>
            </div>

        </div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值