development

Spring MVC 3에서 페이지 네이션을 구현하는 방법

big-blog 2020. 12. 29. 08:20
반응형

Spring MVC 3에서 페이지 네이션을 구현하는 방법


Spring MVC에서 페이지 매김에 사용할 수있는 즉시 사용 가능하고 구현하기 쉬운 표준 페이지 매김 구성 요소 / 태그 라이브러리 또는 코드 샘플이 있습니까?


에서 PagedListHolder및 기타 클래스를 살펴보십시오 org.springframework.beans.support.

예를 들어 다음과 같은 몇 가지 예는 샘플의 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);
    }
}

나도 그렇게 할 방법을 찾고 있었지만 표준 구성 요소 또는 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));
    }
}

이제 요청을 수행하면 요청에 http://domain/app/test.html?id=10&page=1&results=30&sortOrder=asc호출기 개체가 포함됩니다.


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.

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.


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.


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


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

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.

ReferenceURL : https://stackoverflow.com/questions/2245035/how-to-implement-pagination-in-spring-mvc-3

반응형