分页和排序,不知道如何迭代提供的"项目" in< forEach>

时间:2016-06-19 00:42:55

标签: jsp pagination spring-data spring-data-jpa jsp-tags

控制器

   @RequestMapping(value ="/employees/pages", method = RequestMethod.GET)
public String showPages(Model model) {
 Pageable pg = new PageRequest(1,10,Direction.ASC, "empNo");    
 Page<Employee> results = this.employeeService.findPagedEmployees(pg);
 model.addAttribute("listemp", results);
 return "/employees/list";
}

的List.jsp

<h2>Employee List</h2>
<c:if test="${!empty listemp}">
    <table class="table table-sm">
    <thead class="thead-inverse">
    <tr>
        <th width="70">EmpNo</th>
        <th width="120">First Name</th>
        <th width="120">Last Name</th>
        <th width="60"> Gender</th>
        <th width="120">Birth Date</th>
        <th width="120">Hire Date</th>
        <th width="60">Edit</th>
        <th width="60">Delete</th>
    </tr>
    </thead>
    <c:forEach items="${listemp}" var="employee">
        <tr>
            <td>${employee.empNo}</td>
            <td>${employee.firstName}</td>
            <td>${employee.lastName}</td>
            <td>${employee.gender}</td>
            <td>${employee.birthDate}</td>
            <td>${employee.hireDate}</td>
            <td><a href="/welcome/employees/edit/${employee.empNo}" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit</a></td>
<%--            <a href="/welcome/employees/edit/${employee.empNo}" class="btn btn-warning">Edit</a> </td> --%>
            <td><a href="/welcome/employees/delete/${employee.empNo}" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span>Delete</a> </td>
        </tr>
    </c:forEach>
    </table>
</c:if>

我正在尝试通过empNo实现分页和排序。在我调试时,我可以看到员工的详细信息是&#34;结果&#34;,但不知道如何通过页面迭代这些字段。请帮我看看。

我的想法是让每个页面包含101条记录的10个结果。以及上一个和下一个选项。请帮忙!!!

1 个答案:

答案 0 :(得分:3)

您正在使用静态排序,因为您已经传入了要检索的大小和页面。

对于每件商品,您都没有提取内容。

尝试这样的事情......

    <c:forEach items="${listemp.content}" var="employee">

   <tr>
           <td>${employee.empNo}</td>
            <td>${employee.firstName}</td>..........

  </tr>

    <c:if test="${!listemp.last}">
       <li class="next">
       <a href="?page=${listemp.number+1}">Next &rarr;</a>
        </li>
    </c:if>

并在您的控制器类中......

@RequestMapping(value ="/employees/pages", method = RequestMethod.GET)
public String showPages(Model model,@PageableDefault(page = 0,size = 20,direction = Direction.ASC, sort = {"empNo"}) Pageable pg) {
         Page<Employee> results = this.employeeService.findPagedEmployees(pg);
         model.addAttribute("listemp", results);
         return "/employees/page";
        }