分页

来源:互联网 发布:查看svn端口号命令 编辑:程序博客网 时间:2024/06/06 01:35

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" onclick="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" onclick="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>