SpringBoot?把PageHelper分頁(yè)信息返回給前端的方法步驟
第1步:定義線程容器收納HttpHeaders和HttpStatus
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
public class ResponseUtils {
private static ThreadLocal<HttpHeaders> ThreadLocalHeaders = new InheritableThreadLocal<>();
private static ThreadLocal<HttpStatus> ThreadLocalStatus = new InheritableThreadLocal<>();
public static void addHeaders(String key, String value) {
if (ThreadLocalHeaders.get() == null) {
ThreadLocalHeaders.set(new HttpHeaders());
}
ThreadLocalHeaders.get().add(key, value);
}
public static void setResponseCode(HttpStatus httpStatus) {
ThreadLocalStatus.set(httpStatus);
}
public static HttpHeaders getHeaders() {
return ThreadLocalHeaders.get();
}
public static HttpStatus getResponseCode() {
return ThreadLocalStatus.get();
}
public static void clear() {
if (ThreadLocalHeaders.get() != null) {
ThreadLocalHeaders.remove();
}
if (ThreadLocalStatus.get() != null) {
ThreadLocalStatus.remove();
}
}
}
第2步:分頁(yè)信息 轉(zhuǎn)儲(chǔ)到 線程容器 方法的封裝
import com.github.pagehelper.PageInfo;
import com.zhangziwa.practisesvr.utils.response.ResponseUtils;
public class PageHeaderUtils {
private static final String PAGE_NUM = "page_num"; // 當(dāng)前第幾頁(yè)
private static final String PAGE_SIZE = "page_size"; // 每頁(yè)顯示的條數(shù)
private static final String PREV_PAGE = "prev_page"; // 上一頁(yè)頁(yè)碼
private static final String NEXT_PAGE = "next_page"; // 下一頁(yè)頁(yè)碼
private static final String TOTAL_COUNT = "total_count"; // 總條數(shù)
private static final String TOTAL_PAGE = "total_page"; // 總頁(yè)數(shù)
public static <E> void setPageHeader(PageInfo<E> page) {
if (page == null) {
return;
}
ResponseUtils.addHeaders(PAGE_NUM, String.valueOf(page.getPageNum()));
ResponseUtils.addHeaders(PAGE_SIZE, String.valueOf(page.getPageSize()));
ResponseUtils.addHeaders(TOTAL_COUNT, String.valueOf(page.getTotal()));
ResponseUtils.addHeaders(TOTAL_PAGE, String.valueOf(page.getPages() == 0 ? 1 : page.getPages()));
// page.getPages()=1表示就1頁(yè),前后頁(yè)都不存在,故也算特殊場(chǎng)景.也為了page.getPages()+1和page.getPages()-1不會(huì)對(duì)[1,page.getPages()]越界
if (page.getPages() == 0 || page.getPages() == 1) {
ResponseUtils.addHeaders(PREV_PAGE, "");
ResponseUtils.addHeaders(NEXT_PAGE, "");
} else if (page.getPageNum() == 1) {
ResponseUtils.addHeaders(PREV_PAGE, "");
ResponseUtils.addHeaders(NEXT_PAGE, String.valueOf(page.getPages() + 1));
} else if (page.getPageNum() == page.getPages()) {
ResponseUtils.addHeaders(PREV_PAGE, String.valueOf(page.getPages() - 1));
ResponseUtils.addHeaders(NEXT_PAGE, "");
} else {
ResponseUtils.addHeaders(PREV_PAGE, String.valueOf(page.getPages() - 1));
ResponseUtils.addHeaders(NEXT_PAGE, String.valueOf(page.getPages() + 1));
}
}
}
第3步:分頁(yè)查詢,HttpHeaders和HttpStatus收集到線程容器中
public List<Student> listStudents(Integer pageNum, Integer PageSize) {
PageHelper.startPage(PageUtils.getPageNum(pageNum), PageUtils.getPageSize(PageSize), PageUtils.isQueryTotalCount());
PageHelper.orderBy("age asc");
List<Student> students = userMapper.listStudents();
PageInfo<Student> studentPageInfo = PageInfo.of(students);
// 收集分頁(yè)信息到 ThreadLocal
PageHeaderUtils.setPageHeader(studentPageInfo);
// 收集HttpStatus到 ThreadLocal
// ResponseUtils.setResponseCode(num2HttpStatus("200")); // 為了使用一下num2HttpStatus方法
ResponseUtils.setResponseCode(HttpStatus.OK);
return students;
}
public class HttpStatusUtils {
public static HttpStatus num2HttpStatus(String num) {
HttpStatus httpStatus = HttpStatus.NOT_FOUND;
for (HttpStatus status : HttpStatus.values()) {
if (Integer.parseInt(num) == status.value()) {
return status;
}
}
return httpStatus;
}
}
第4步:controller層需為@RestController Restful接口
@RestController
@Slf4j
public class SearchController {
@Autowired
UserService userService;
@RequestMapping(value = "/getAllStudents", method = RequestMethod.GET)
public List<Student> login() {
List<Student> students = userService.listStudents(1, 10);
students.forEach(System.out::println);
return students;
}
}
第5步:線程容器收納HttpHeaders和HttpStatus添加到ServerHttpResponse
@ControllerAdvice
public class HttpResponseBodyAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class clazz,
ServerHttpRequest request, ServerHttpResponse response) {
HttpHeaders headers = response.getHeaders();
// 分頁(yè)信息添加到ServerHttpResponse
HttpHeaders headersContext = ResponseUtils.getHeaders();
if (nonNull(headersContext) && !headersContext.isEmpty()) {
headers.addAll(headersContext);
}
// 狀態(tài)碼添加到ServerHttpResponse
if (nonNull(ResponseUtils.getResponseCode())) {
response.setStatusCode(ResponseUtils.getResponseCode());
}
return body;
}
}
第6步:測(cè)試結(jié)果


到此這篇關(guān)于SpringBoot 把PageHelper分頁(yè)信息返回給前端的方法步驟的文章就介紹到這了,更多相關(guān)SpringBoot PageHelper分頁(yè)信息返回內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot3解決跨域請(qǐng)求的方案小結(jié)
解決跨域請(qǐng)求,主要有JSONP,iframe,window.name,CORS等方式,其中CORS方式是最常用的跨域?qū)崿F(xiàn)方式,而且是對(duì)各種請(qǐng)求方法、各種數(shù)據(jù)請(qǐng)求類型都是完美支持的,本文介紹了SpringBoot3解決跨域請(qǐng)求的方案小結(jié),需要的朋友可以參考下2024-07-07
Idea開發(fā)工具之SpringBoot整合JSP的過(guò)程
最近在學(xué)習(xí)SpringBoot,看到SpringBoot整合jsp,順帶記錄一下。本文通過(guò)圖文實(shí)例相結(jié)合給大家講解SpringBoot整合JSP的過(guò)程,感興趣的朋友一起看看吧2021-09-09
RestTemplate報(bào)錯(cuò)I/O?error?on?POST?request?for的解決辦法
這篇文章主要給大家介紹了關(guān)于RestTemplate報(bào)錯(cuò)I/O?error?on?POST?request?for的解決辦法,文中通過(guò)代碼實(shí)例將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
Java利用Socket和IO流實(shí)現(xiàn)文件的上傳與下載
本文主要介紹了Java利用Socket和IO流實(shí)現(xiàn)文件的上傳與下載,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
淺談mybatis mapper.xml文件中$和#的區(qū)別
這篇文章主要介紹了淺談mybatis mapper.xml文件中$和#的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
JSP 開發(fā)之hibernate的hql查詢多對(duì)多查詢
這篇文章主要介紹了JSP 開發(fā)之hibernate的hql查詢多對(duì)多查詢的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09

