Mình có tạo một web danh sách bằng cách phân trang
<div th:if="${message != null}" class ="alert alert-danger" role="alter">
<i>[[${message}]]</i>
</div>
<div class="row mt-2 mb-2">
<div class="col">
<form th:action="@{/admin/class/search/paginate}">
<div class="form-inline float-left">
<label for="name">Name:</label>
<input type="text" class="form-control ml-2" name="name"
id="name" placeholder="Name"/>
<button class="btn btn-outline-primary ml-2">Search</button>
</div>
</form>
<div class="float-right">
<a class="btn btn-outline-primary" th:href="@{/admin/class/addOrEdit}">Add New Info Student</a>
</div>
</div>
</div>
<div class="row" th:if="${!classPage.hasContent()}">
<div class="col" >
<div class="alert alert-danger" role="alert">
<strong>No Info Student Found</strong>
</div>
</div>
</div>
<table class="table" th:if="${classPage.hasContent()}">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="class, iStat :${classPage.content}">
<th scope="row">[[${class.id}]]</th>
<td colspan="2" th:text="${class.name}"></td>
<td>
<a th:href="@{'/admin/class/edit/' + ${class.id}} " class="btn btn-outline-info">Edit</a>
<a th:href="@{'/admin/class/delete/' + ${class.id}} " class="btn btn-outline-info">Delete</a>
</td>
</tr>
</tbody>
</table>
<div class="text-muted">
<nav aria-label="Page navigation" th:if="${classPage.totalPages>0}">
<ul class="pagination justify-content-center">
<li th:class="${1== classPage.number +1}? 'page-item active':'page-item'">
<a th:href="@{/admin/class/(name=${name},size=${classPage.size},page=${1})}"
class="page-link">First
</a>
</li>
<li class="page-item active"
th:each="pageNumber : ${pageNumbers}"
th:if="${classPage.totalPages > 1}"
th:class="${pageNumber == classPage.number +1}? 'page-item active' : 'page-item'">
<a th:href="@{/admin/class/(name=${name},size=${classPage.size},page=${pageNumber})}"
class="page-link"
th:text="${pageNumber}">
</a>
</li>
<li th:class="${classPage.totalPages== classPage.number +1}? 'page-item active':'page-item'">
<a th:href="@{/admin/class/(name=${name},size=${classPage.size},page=${classPage.totalPages})}"
class="page-link">Last
</a>
</li>
</ul>
</nav>
</div>
Mọi thứ đều ổn cho đến khi test chức năng xóa
@RequestMapping("delete/{id}")
public String delete(ModelMap model, Classes entity, ClassDto dto
,@PathVariable("id")Integer id) {
Optional<Classes> opt = classService.findById(id);
if(opt.isPresent()) {
classService.deleteById(id);
model.addAttribute("message", "Delete is success");
}else {
model.addAttribute("message", "Class not found");
}
return "admin/class/list";
}
Và thông báo lỗi như sau
Exception evaluating SpringEL expression: "!classPage.hasContent()
EL1011E: Method call: Attempted to call method hasContent() on null context object