Search code examples
jsppaginationspring-dataspring-data-jpajsp-tags

Pagination and Sorting, Don't know how to iterate over supplied "items" in <forEach>


Controller

   @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>

I am trying to implement pagination and sorting by empNo. While I debug I can see the employee details are "results", but not sure how to iterate those fields through pages. please help me with view.

My idea is to like have each page with 10 results for 191 records. and previous and next options. please help !!!


Solution

  • You are using static sorting, since you already passed in the size and page that you want to retrieve.

    for each items, you didn't pull out the content.

    Try something like this...

        <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>
    

    and in your controller class...

    @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";
            }