如何在 Spring MVC 3 中实现分页 [英] How to implement pagination in Spring MVC 3
本文介绍了如何在 Spring MVC 3 中实现分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何开箱即用、易于实现的标准分页组件/标签库或代码示例可用于 Spring MVC 中的分页?
Is there any out-of-the-box, easy to implement, standard pagination component/tag-lib or code-sample available for pagination in Spring MVC?
推荐答案
看看 PagedListHolder
和来自 org.springframework.beans.support
.
请参阅示例中的 JPetstore 以获取一些示例,例如在 SearchProductsController.java
:
See the JPetstore in the samples for some examples, e.g. in SearchProductsController.java
:
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String keyword = request.getParameter("keyword");
if (keyword != null) {
if (!StringUtils.hasLength(keyword)) {
return new ModelAndView("Error", "message", "Please enter a keyword to search for, then press the search button.");
}
PagedListHolder productList = new PagedListHolder(this.petStore.searchProductList(keyword.toLowerCase()));
productList.setPageSize(4);
request.getSession().setAttribute("SearchProductsController_productList", productList);
return new ModelAndView("SearchProducts", "productList", productList);
}
else {
String page = request.getParameter("page");
PagedListHolder productList = (PagedListHolder) request.getSession().getAttribute("SearchProductsController_productList");
if (productList == null) {
return new ModelAndView("Error", "message", "Your session has timed out. Please start over again.");
}
if ("next".equals(page)) {
productList.nextPage();
}
else if ("previous".equals(page)) {
productList.previousPage();
}
return new ModelAndView("SearchProducts", "productList", productList);
}
}
这篇关于如何在 Spring MVC 3 中实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文