Spring mvc 分页

发布于 2024-11-07 04:25:53 字数 1803 浏览 0 评论 0原文

你好,我在我的项目中使用 jqgrid,我需要实现分页,所以我在 php 中找到了一个代码并将其复制到 java :

@RequestMapping(value = "getgriddata", method = RequestMethod.GET)
    public @ResponseBody
    String getGrid(@RequestParam("page") String page, @RequestParam("rows") int rownumber, @RequestParam("sidx") String sortBy, @RequestParam("sord") String sortOrder,@RequestParam(value = "_search") String search, @RequestParam(value="filters", required = false) String filters) {

        int totalCount = dao.getTotalRecordCount();

        CustomJsonResponse response = new CustomJsonResponse();

        int totalPages = 0;

        if (totalCount > 0) {
            totalPages = (int) Math.ceil(totalCount/rownumber);
        }

        if (Integer.valueOf(page) > totalPages) {
            page = String.valueOf(totalPages);
        }

        Gson jsonConverter = new Gson();

        int start = (rownumber * Integer.valueOf(page)) - rownumber;
        Filters searchFilter = null;

        if(Boolean.valueOf(search) == true){
            searchFilter = jsonConverter.fromJson(filters, Filters.class);
        }

        // Retrieve records from database
        List<Record> recorBatch = dao.getRecords(start, rownumber, sortBy, sortOrder, search, searchFilter);

        // Assign the result from the service to this response
        response.setRows(recorBatch);
        response.setTotal(String.valueOf(totalPages));
        response.setRecords(String.valueOf(stagingLoadBatch.size()));
        response.setPage(page);

        return jsonConverter.toJson(response);
    }

当我处理整个数据集时,这可以正常工作,但是当我缩小范围时搜索,即选择某个时间间隔内的记录,然后分页失败。我意识到这是不好的做法,我从这段代码中复制了它以进行分页:

http://pastebin.com/ybKSXzyq

有人可以为此提出更好的解决方案吗?

Hello I'm using jqgrid for my project, and I needed to implement the pagination so I found a code in php and copied it over to java :

@RequestMapping(value = "getgriddata", method = RequestMethod.GET)
    public @ResponseBody
    String getGrid(@RequestParam("page") String page, @RequestParam("rows") int rownumber, @RequestParam("sidx") String sortBy, @RequestParam("sord") String sortOrder,@RequestParam(value = "_search") String search, @RequestParam(value="filters", required = false) String filters) {

        int totalCount = dao.getTotalRecordCount();

        CustomJsonResponse response = new CustomJsonResponse();

        int totalPages = 0;

        if (totalCount > 0) {
            totalPages = (int) Math.ceil(totalCount/rownumber);
        }

        if (Integer.valueOf(page) > totalPages) {
            page = String.valueOf(totalPages);
        }

        Gson jsonConverter = new Gson();

        int start = (rownumber * Integer.valueOf(page)) - rownumber;
        Filters searchFilter = null;

        if(Boolean.valueOf(search) == true){
            searchFilter = jsonConverter.fromJson(filters, Filters.class);
        }

        // Retrieve records from database
        List<Record> recorBatch = dao.getRecords(start, rownumber, sortBy, sortOrder, search, searchFilter);

        // Assign the result from the service to this response
        response.setRows(recorBatch);
        response.setTotal(String.valueOf(totalPages));
        response.setRecords(String.valueOf(stagingLoadBatch.size()));
        response.setPage(page);

        return jsonConverter.toJson(response);
    }

This works ok when I'm working with entire set of data, but when I narrow the search, i.e. select records in some time interval, then the pagination breaks down. I realized this is bad practice, I've copied it from this code for pagination:

http://pastebin.com/ybKSXzyq

Can anybody suggest better solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜妞爱困 2024-11-14 04:25:53

我建议您查看显示标签库进行分页。

I would suggest you to look at Display Tag Library, for pagination.

蓝眼睛不忧郁 2024-11-14 04:25:53

我正在完整实现它,我做了 JQGridPage.java 类来包含 jqgrid 的数据

public class JQGridPage<T> {

    private Integer page;
    private Integer total;
    private Long records;
    private List<T> rows;
....
}

,并在控制器方法中填充它并将其作为方法的返回发送

@RequestMapping(value = "", method = RequestMethod.GET, params = "page")
@ResponseBody
public JQGridPage<T> listPages(
        @RequestParam(value = "page", defaultValue = "1") int page,
        @RequestParam(value = "rows", defaultValue = "10") int rows,
        @RequestParam(value = "sidx", defaultValue = "id") String sortField,
        @RequestParam(value = "sord", defaultValue = "ASC") String sortDirection,
        @RequestParam(value = "searchField", defaultValue = "id") String searchField,
        @RequestParam(value = "searchOper", defaultValue = "eq") String searchOper,
        @RequestParam(value = "searchString", defaultValue = "") String searchString) {
.....
    JQGridPage<T> jqGrid = new JQGridPage<T>();
    jqGrid.setPage(page);
    jqGrid.setTotal(pageOfCustomer.getTotalPages());
    jqGrid.setRecords(pageOfCustomer.getTotalElements());
    jqGrid.setRows(pageOfCustomer.getContent());
    return jqGrid;
}

I'm doing full implementation of it I did JQGridPage.java class to contain data for jqgrid as

public class JQGridPage<T> {

    private Integer page;
    private Integer total;
    private Long records;
    private List<T> rows;
....
}

and in controller method I'm filling it and send it as return of method

@RequestMapping(value = "", method = RequestMethod.GET, params = "page")
@ResponseBody
public JQGridPage<T> listPages(
        @RequestParam(value = "page", defaultValue = "1") int page,
        @RequestParam(value = "rows", defaultValue = "10") int rows,
        @RequestParam(value = "sidx", defaultValue = "id") String sortField,
        @RequestParam(value = "sord", defaultValue = "ASC") String sortDirection,
        @RequestParam(value = "searchField", defaultValue = "id") String searchField,
        @RequestParam(value = "searchOper", defaultValue = "eq") String searchOper,
        @RequestParam(value = "searchString", defaultValue = "") String searchString) {
.....
    JQGridPage<T> jqGrid = new JQGridPage<T>();
    jqGrid.setPage(page);
    jqGrid.setTotal(pageOfCustomer.getTotalPages());
    jqGrid.setRecords(pageOfCustomer.getTotalElements());
    jqGrid.setRows(pageOfCustomer.getContent());
    return jqGrid;
}
本宫微胖 2024-11-14 04:25:53

这是我在 Spring MVC 中的分页结果
image1

image2

image3

This is my pangination result in Spring MVC
image1

image2

image3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文