Spring分页,如何在Uri中修改参数

时间:2016-11-04 23:29:46

标签: java html spring spring-mvc pagination

我正在编写一个简单的基于Spring的webapp,我在实现分页时遇到了问题。

适用于我的控制器方法:

var f=d=>
  d.forEach(k=>
    alert(k)
  );
let a=[1,2];
f(a);

观点:

@RequestMapping(value = "/search", method = RequestMethod.GET)
public ModelAndView search(@ModelAttribute String ss, @RequestParam Integer page, HttpServletRequest request){

    String author = request.getParameter("author");
    String tittle = request.getParameter("tittle");
    String isbn = request.getParameter("ISBN");
    String language = request.getParameter("language");
    Integer pagesFrom;
    Integer pagesTo;
    Integer quantityFrom;
    Integer quantityTo;

    try{
        pagesFrom = Integer.parseInt(request.getParameter("numberOfPagesFrom"));
    } catch (NumberFormatException e){
        pagesFrom = Integer.valueOf(0);
    }
    try{
        pagesTo = Integer.parseInt(request.getParameter("numberOfPagesTo"));
    } catch (NumberFormatException e){
        pagesTo = Integer.MAX_VALUE;
    }

    try{
        quantityFrom = Integer.parseInt(request.getParameter("quantityFrom"));
    } catch (NumberFormatException e){
        quantityFrom = Integer.valueOf(0);
    }
    try{
        quantityTo = Integer.parseInt(request.getParameter("quantityTo"));
    } catch (NumberFormatException e){
        quantityTo = Integer.MAX_VALUE;
    }

    Predicate predicate = book.author.containsIgnoreCase(author)
            .and(book.tittle.containsIgnoreCase(tittle))
            .and(book.ISBN.contains(isbn))
            .and(book.language.contains(language))
            .and(book.numberOfPages.between(pagesFrom, pagesTo))
            .and(book.bookQuantity.quantity.between(quantityFrom, quantityTo));

    List<Book> books = bookRepository.findAll(predicate);

    PagedListHolder pagedListHolder = new PagedListHolder(bookRepository.findAll(predicate));
    pagedListHolder.setPage(page);
    pagedListHolder.setPageSize(5);
    ModelAndView mav = new ModelAndView("searchResult");
    mav.addObject("books", pagedListHolder.getPageList());

    return mav;
}

据我设法将隐藏变量页传递给0.我不知道如何做其他页面。例如,点击

<#ftl encoding='UTF-8'>
<#import "/spring.ftl" as spring/>
<html>
<head>

    <script>
        function goBack() {
            window.history.back();
        }
    </script>

    <link href="../../resources/styles/bootstrap.min.css" rel="stylesheet">
    <link href="../../resources/styles/bootstrap-flex.min.css" rel="stylesheet">
    <link href="../../resources/styles/bootstrap-grid.min.css" rel="stylesheet">
    <link href="../../resources/styles/bootstrap-reboot.min.css" rel="stylesheet">
    <link href="../../resources/javascript/jquery-3.1.1.min.js" rel="script">
    <link href="../../resources/javascript/bootstrap.min.js" rel="script">
    <link href="../../resources/javascript/npm.js" rel="script">
    <style>
        #goBackButton{
            text-align: center;
        }
        h2{
            margin: 10px;
            text-align: center;
        }
        #books{
            width: 70%;
            margin-left: 15%;
        }
    </style>
</head>

<body>
<header>
</header>
<h2>Lista szukanych książek</h2>
<table id="books" class="table table-bordered table-hover">
    <thead>
    <tr>
        <td>ID</td>
        <td>Autor</td>
        <td>Tytuł</td>
        <td>ISBN</td>
        <td>Język</td>
        <td>Ilość stron</td>
        <td>Ilość</td>
    </tr>
    </thead>
    <tbody>
    <tr>
    <#if books?has_content>
        <#list books as item>
        <tr>
            <td>${item.id}</td>
            <td>${item.author}</td>
            <td>${item.tittle}</td>
            <td>${item.ISBN}</td>
            <td>${item.language}</td>
            <td>${item.numberOfPages}</td>
            <td>${item.bookQuantity.quantity}</td>
        </tr>
        </#list>
    </#if>

    </tr>

    </tbody>

</table>

<div class="container-fluid">

    <nav>
        <ul class="pagination">
            <li class="page-item">
                <a href="#" class="page-link" aria-label="Previous">
                    <span aria-hidden="true">«</span>
                </a>
            </li>
            <li class="page-item"><a class="page-link" href="page=1">2</a></li>
            <li class="page-item"><a class="page-link" href="#">3</a></li>
            <li class="page-item"><a class="page-link" href="#">4</a></li>
            <li class="page-item"><a class="page-link" href="#">5</a></li>

            <li class="page-item">
                <a href="#" class="page-link" aria-label="Next">
                    <span aria-hidden="true">»</span>
                </a>
            </li>
        </ul>
    </nav>

</div>

<button id="goBackButton" type="button" class="btn btn-link" onclick="goBack()">Go Back</button>
</body>
</html>

URI不会更改,只有页面参数为ceratin编号。 感谢任何线索,也批评。

1 个答案:

答案 0 :(得分:0)

我只是通过重定向到另一个只显示确切页面的方法解决了我的问题。