分页和排序,不知道如何遍历 <forEach> 中提供的 "items"
Pagination and Sorting, Don't know how to iterate over supplied "items" in <forEach>
控制器
@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 实现分页和排序。在调试时,我可以看到员工详细信息是 "results",但不确定如何在页面中迭代这些字段。请帮我看看。
我的想法是让每个页面有 10 个结果,共 191 条记录。以及上一个和下一个选项。请帮助!!!
您正在使用静态排序,因为您已经传入了要检索的大小和页面。
对于每个项目,您都没有提取内容。
尝试这样的事情...
<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 →</a>
</li>
</c:if>
并在您的控制器中 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";
}
控制器
@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 实现分页和排序。在调试时,我可以看到员工详细信息是 "results",但不确定如何在页面中迭代这些字段。请帮我看看。
我的想法是让每个页面有 10 个结果,共 191 条记录。以及上一个和下一个选项。请帮助!!!
您正在使用静态排序,因为您已经传入了要检索的大小和页面。
对于每个项目,您都没有提取内容。
尝试这样的事情...
<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 →</a>
</li>
</c:if>
并在您的控制器中 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";
}