SSSP项目-分页操作的实现

  • A+
所属分类:JAVA

分页操作的设计实现

service层:

可以直接调用jparepository的pageable来实现

实现方式:

  1. /**
  2.      * 获取指定分页的员工
  3.      * @param pageNum
  4.      * @param pageSize
  5.      * @return
  6.      */
  7.     @Transactional(readOnly=true)
  8.     public Page<Employee> getPage(int pageNum,int pageSize){
  9.         //分页的起始页码从0开始
  10.         PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize);
  11.         return employeeRepository.findAll(pageRequest);
  12.     }

controller层

可以使用@RequestParam获取用户选取的页码来确定目前是第几页

具体实现如下:

  1. @RequestMapping("/emplistpage")
  2.     public String emplistpage(
  3.             @RequestParam(value="pageNum",required=false,defaultValue="1")String pageNumStr
  4.             ,Map<String,Object> map) {
  5.         int pageNum = 1;
  6.         pageNum = Integer.parseInt(pageNumStr);
  7.         //针对页码小于1以及负数的情况处理
  8.         if(pageNum < 1) {
  9.             pageNum = 1;
  10.         }
  11.         Page<Employee> page = employeeService.getPage(pageNum, 1);
  12.         map.put("page", page);
  13.         return "employee/list";
  14.     }

view页面

使用page.content来获取内容,使用<c:>标签来循环输出页面

具体实现:

  1. <tbody>
  2.                 <c:forEach items="${page.content }" var="employee">
  3.                     <tr>
  4.                         <th>${employee.id }</th>
  5.                         <td>${employee.name }</td>
  6.                         <td>${employee.email }</td>
  7.                         <td><fmt:formatDate value="${employee.birth }" pattern="yyyy-MM-dd" /> </td>
  8.                         <td><fmt:formatDate value="${employee.entryTime }" pattern="yyyy-MM-dd hh:mm:ss" /></td>
  9.                         <td>${employee.supervisor.name }</td>
  10.                         <td>${employee.salary }</td>
  11.                         <td>${employee.dept.deptName }</td>
  12.                         <td><a class="btn btn-info" href="">编辑</a></td>
  13.                         <td><a class="btn btn-danger" href="">删除</a></td>
  14.                     </tr>
  15.                 </c:forEach>
  16.             </tbody>
  17.         </table>
  18.         <!-- 分页组件 -->
  19.         <div class="text-center">
  20.             <nav aria-label="Page navigation">
  21.               <ul class="pagination">
  22.                 <c:if test="${ (page.number + 1) != 1 }">
  23.                     <li>
  24.                       <a href="<%=path %>/emplistpage?pageNum=${page.number + 1 - 1}">
  25.                         <span aria-hidden="true">&laquo;</span>
  26.                       </a>
  27.                     </li>
  28.                 </c:if>
  29.                 <li><a href="<%=path %>/emplistpage?pageNum=1 ">1</a></li>
  30.                 <li><a href="<%=path %>/emplistpage?pageNum=2 ">2</a></li>
  31.                 <li><a href="<%=path %>/emplistpage?pageNum=3 ">3</a></li>
  32.                 <li><a href="<%=path %>/emplistpage?pageNum=4 ">4</a></li>
  33.                 <li><a href="<%=path %>/emplistpage?pageNum=5 ">5</a></li>
  34.                 <c:if test="${(page.number + 1) != page.totalPages  }">
  35.                     <li>
  36.                       <a href="<%=path %>/emplistpage?pageNum=${page.number + 1 + 1}">
  37.                         <span aria-hidden="true">&raquo;</span>
  38.                       </a>
  39.                     </li>
  40.                 </c:if>
  41.               </ul>
  42.             </nav>
  43.         </div>

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: