How to PageRequest in SpringBoot and Jsp หวังว่าจะเป็นประโยชน์นะครับ
Code (Java)
[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";
}
/*Service*/
public Page<StudentEntity> listAll(PageRequest pageRequest) {
return (Page<StudentEntity>) studentRepository.listAll(pageRequest);
}
/*Repository*/
@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;
}
/*StudentEntity*/
@Data
@Entity
@Table(name = "STUDENT")
public class StudentEntity {
@Id
@SequenceGenerator(name="SEQ_STUDENT", sequenceName="seqStudent", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqStudent")
@Column(name = "id_student", unique = true, nullable = false, insertable = true, updatable = true)
private BigDecimal idStudent;
@Column(name = "f_name")
private String fname;
@Column(name = "l_name")
private String lname;
@Column
private String address;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_DEPARTMENT")
private DepartmentEntity department;
}
/*.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>Tag : Java, HTML, JavaScript, JAVA
ประวัติการแก้ไข 2020-01-22 10:33:13
Date :
2020-01-22 10:31:02
By :
MyLove
View :
893
Reply :
1
ตัวอย่าง
Date :
2020-01-22 10:32:06
By :
MyLove
Load balance : Server 04