【问题标题】:Spring mvc paginationSpring mvc 分页
【发布时间】:2017-10-24 22:49:42
【问题描述】:

您好,我的项目使用的是 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

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

【问题讨论】:

    标签: java spring spring-mvc pagination


    【解决方案1】:

    我建议你查看Display Tag Library,进行分页。

    【讨论】:

    • 我正在使用 jqgrid 不显示库,tx
    • @London:是的,但在我看来,它并没有解决您的分页问题,​​因为您自己提到您使用 PHP 代码进行了黑客攻击。那么,为什么不选择 Java 呢?
    • 因为我几乎完成了这个项目但没有考虑这个分页,认为它一直在工作我现在无法切换所有逻辑。
    • 再次从php中找到了正确的解决方案,这次更正stackoverflow.com/questions/2060399/pagination-in-php
    • @London:很高兴知道这一点。干杯。
    【解决方案2】:

    我正在完全实现它我做了 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;
    }
    

    【讨论】:

      【解决方案3】:

      这是我在 Spring MVC 中的拼图结果

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-21
        • 2017-06-17
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 2013-04-08
        • 1970-01-01
        相关资源
        最近更新 更多