|
|
|
How to PageRequest in SpringBoot and Jsp หวังว่าจะเป็นประโยชน์นะครับ |
|
|
|
|
|
|
|
Code (Java)
/*Controller*/
@GetMapping("/student")
public String showStudent(ModelMap model,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "3") int size){
Page<StudentEntity> pages = studentEntityService.listAll(PageRequest.of(page, size));
model.addAttribute("number", pages.getNumber());
model.addAttribute("totalPages", pages.getTotalPages());
model.addAttribute("totalElements", pages.getTotalElements());
model.addAttribute("size", pages.getSize());
model.addAttribute("data",pages.getContent());
return "jsp/student/student";
}
Tag : HTML, HTML5, JAVA
|
ประวัติการแก้ไข 2020-01-22 10:43:22
|
|
|
|
|
Date :
2020-01-22 10:38:26 |
By :
MyLove |
View :
665 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
/*Service*/
public class StudentEntityService {
@Autowired StudentRepository studentRepository;
public Page<StudentEntity> listAll(PageRequest pageRequest) {
return (Page<StudentEntity>) studentRepository.listAll(pageRequest);
}
|
|
|
|
|
Date :
2020-01-22 10:39:33 |
By :
MyLove |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
/*Repository*/
public class StudentRepository {
@PersistenceContext
private EntityManager entityManager;
public Page<StudentEntity> listAll(PageRequest pageRequest) {
Query query = entityManager.createNativeQuery("SELECT * FROM STUDENT",StudentEntity.class);
query.setFirstResult(pageRequest.getPageNumber());
query.setMaxResults(pageRequest.getPageSize());
Page<StudentEntity> page = new PageImpl<>(query.getResultList());
return page;
}
}
|
|
|
|
|
Date :
2020-01-22 10:41:07 |
By :
MyLove |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
/*Jsp*/
<c:if test="${data.size() > 0 }">
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="student?page=0 &size=${size}"class="page-link">Previous</a></li>
<c:forEach begin="0" end="${totalPages+1}" var="page">
<li class="page-item"><a class="page-link" href="student?page=${page}&size=${size}" class="page-link">${page+1}</a></li>
</c:forEach>
<li class="page-item"><a class="page-link" href="student?page=${totalPages+1}&size=${size}">LastPage</a></li>
</ul>
</nav>
Showing ${number+1} of ${size} of ${totalElements}
</c:if>
|
ประวัติการแก้ไข 2020-01-22 10:45:53
|
|
|
|
Date :
2020-01-22 10:45:11 |
By :
MyLove |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่างภาพ
|
|
|
|
|
Date :
2020-01-22 10:46:40 |
By :
MyLove |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|