如何在Spring MVC 3中实现分页

时间:2021-10-24 14:32:15

Is there any out-of-the-box, easy to implement, standard pagination component/tag-lib or code-sample available for pagination in Spring MVC?

在Spring MVC中,是否存在可用于分页的开箱即用的、易于实现的、标准的分页组件/标记库或代码示例?

6 个解决方案

#1


87  

Have a look at PagedListHolder and other classes from org.springframework.beans.support.

查看PagedListHolder和org.springframe .beans.support中的其他类。

See the JPetstore in the samples for some examples, e.g. in SearchProductsController.java:

参见示例中的JPetstore,了解一些示例,例如在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);
    }
}

#2


11  

I was looking for a way to do that, too, but didn't find any standard component or taglib. I think mainly because paging can become very specific since you need to retrieve your data with paging from the database already (if you are using Hibernate you can easily do that using the Criteria API). I came up with something like this:

我也在寻找这样做的方法,但是没有找到任何标准组件或taglib。我认为主要是因为分页可以变得非常特定,因为您需要使用数据库中的分页来检索数据(如果您正在使用Hibernate,那么可以使用Criteria API轻松地检索数据)。我想到了这样的东西:

public class Pager
{
    private int page;
    private int results;
    private String sortOrder;
    private String sortColumn;

    // Getters and setters
}

@Controller
public class StuffController
{
    @Autowired SomeEntityService someEntityService;

    @RequestMapping("/test.html", method = Method.GET)
    public void getStuffPaged(@RequestParam("id") String id, Pager pager, ModelMap mm)
    {
        mm.addAttribute("entities", someEntityService.get(id, pager));
    }
}

If you now perform a request to http://domain/app/test.html?id=10&page=1&results=30&sortOrder=asc you will get the pager Object in your request.

如果您现在对http://domain/app/test.html?id=10&page=1&results=30&sortOrder=asc你将在你的请求中获得寻呼机对象。

#3


4  

No one comes to mind and Google also doesn't reveal any specific components for that (although it gives pretty much concrete examples and hints). But in theory just a bunch of buttons and one (or two) request parameters are more than sufficient. Then let the SQL/DB do its work. I've posted an answer to similar question in JSP/Servlet/DAO context before here.

没有人想到谷歌,它也没有透露任何具体的组件(尽管它给出了很多具体的例子和提示)。但在理论上,仅仅一堆按钮和一个(或两个)请求参数就足够了。然后让SQL/DB完成它的工作。在此之前,我已经在JSP/Servlet/DAO上下文中给出了类似问题的答案。

It basically boils down to pass firstrow (index of first row to be displayed in a page) around as request parameter and having two buttons/links in the pagination form which in/decrements the firstrow with rowcount (amount of rows displayed at once in a page) in combination with a SQL query which returns a subset of the results with help of under each LIMIT, OFFSET clauses, or subselects, or specific functions, depending on the DB in question. See the aforelinked answer for detailed code examples and SQL queries.

基本上可以归结为通过firstrow(索引第一行要显示的页面)作为请求参数和有两个按钮/链接分页的形式在/衰减firstrow rowcount(数量的行显示在页面一次)结合一个SQL查询返回的结果的一个子集的帮助下每个限制,补偿条款,或子查询,或特定功能,根据数据库的问题。有关详细的代码示例和SQL查询,请参见上述的答案。

#4


4  

Have you ever heard about the Spring Data JPA project? There is a nice flexible solution using the Pagable interface. I've found it to be the simplest way to achieve clean, boilerplate-free pagination. Check out more at the Spring Data JPA homepage.

您听说过Spring Data JPA项目吗?有一个使用可分页接口的灵活的解决方案。我发现这是实现干净、无模板分页的最简单方法。查看Spring Data JPA主页的更多信息。

#5


3  

Here's a link to the Spring Data JPA reference docs, where they have a very clean approach to web pagination.

这里有一个到Spring Data JPA引用文档的链接,在这里他们有一个非常干净的web分页方法。

#6


2  

I published an open source java library focused on pagination with spring framework some time ago.

不久前,我发布了一个专注于使用spring框架进行分页的开放源码java库。

Although it has not been very successful perhaps someone may be interested in trying it.

虽然它不是很成功,也许有人有兴趣尝试它。

There are examples for using it with

这里有使用它的例子

The online examples are somewhat obsolete, better download the jdal-samples file from sourceforge.

在线示例有些过时,最好从sourceforge下载jdal-samples文件。

#1


87  

Have a look at PagedListHolder and other classes from org.springframework.beans.support.

查看PagedListHolder和org.springframe .beans.support中的其他类。

See the JPetstore in the samples for some examples, e.g. in SearchProductsController.java:

参见示例中的JPetstore,了解一些示例,例如在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);
    }
}

#2


11  

I was looking for a way to do that, too, but didn't find any standard component or taglib. I think mainly because paging can become very specific since you need to retrieve your data with paging from the database already (if you are using Hibernate you can easily do that using the Criteria API). I came up with something like this:

我也在寻找这样做的方法,但是没有找到任何标准组件或taglib。我认为主要是因为分页可以变得非常特定,因为您需要使用数据库中的分页来检索数据(如果您正在使用Hibernate,那么可以使用Criteria API轻松地检索数据)。我想到了这样的东西:

public class Pager
{
    private int page;
    private int results;
    private String sortOrder;
    private String sortColumn;

    // Getters and setters
}

@Controller
public class StuffController
{
    @Autowired SomeEntityService someEntityService;

    @RequestMapping("/test.html", method = Method.GET)
    public void getStuffPaged(@RequestParam("id") String id, Pager pager, ModelMap mm)
    {
        mm.addAttribute("entities", someEntityService.get(id, pager));
    }
}

If you now perform a request to http://domain/app/test.html?id=10&page=1&results=30&sortOrder=asc you will get the pager Object in your request.

如果您现在对http://domain/app/test.html?id=10&page=1&results=30&sortOrder=asc你将在你的请求中获得寻呼机对象。

#3


4  

No one comes to mind and Google also doesn't reveal any specific components for that (although it gives pretty much concrete examples and hints). But in theory just a bunch of buttons and one (or two) request parameters are more than sufficient. Then let the SQL/DB do its work. I've posted an answer to similar question in JSP/Servlet/DAO context before here.

没有人想到谷歌,它也没有透露任何具体的组件(尽管它给出了很多具体的例子和提示)。但在理论上,仅仅一堆按钮和一个(或两个)请求参数就足够了。然后让SQL/DB完成它的工作。在此之前,我已经在JSP/Servlet/DAO上下文中给出了类似问题的答案。

It basically boils down to pass firstrow (index of first row to be displayed in a page) around as request parameter and having two buttons/links in the pagination form which in/decrements the firstrow with rowcount (amount of rows displayed at once in a page) in combination with a SQL query which returns a subset of the results with help of under each LIMIT, OFFSET clauses, or subselects, or specific functions, depending on the DB in question. See the aforelinked answer for detailed code examples and SQL queries.

基本上可以归结为通过firstrow(索引第一行要显示的页面)作为请求参数和有两个按钮/链接分页的形式在/衰减firstrow rowcount(数量的行显示在页面一次)结合一个SQL查询返回的结果的一个子集的帮助下每个限制,补偿条款,或子查询,或特定功能,根据数据库的问题。有关详细的代码示例和SQL查询,请参见上述的答案。

#4


4  

Have you ever heard about the Spring Data JPA project? There is a nice flexible solution using the Pagable interface. I've found it to be the simplest way to achieve clean, boilerplate-free pagination. Check out more at the Spring Data JPA homepage.

您听说过Spring Data JPA项目吗?有一个使用可分页接口的灵活的解决方案。我发现这是实现干净、无模板分页的最简单方法。查看Spring Data JPA主页的更多信息。

#5


3  

Here's a link to the Spring Data JPA reference docs, where they have a very clean approach to web pagination.

这里有一个到Spring Data JPA引用文档的链接,在这里他们有一个非常干净的web分页方法。

#6


2  

I published an open source java library focused on pagination with spring framework some time ago.

不久前,我发布了一个专注于使用spring框架进行分页的开放源码java库。

Although it has not been very successful perhaps someone may be interested in trying it.

虽然它不是很成功,也许有人有兴趣尝试它。

There are examples for using it with

这里有使用它的例子

The online examples are somewhat obsolete, better download the jdal-samples file from sourceforge.

在线示例有些过时,最好从sourceforge下载jdal-samples文件。