gpt4 book ai didi

java - Spring分页,如何修改Uri中的参数

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:19 26 4
gpt4 key购买 nike

我正在编写一个简单的基于 spring 的 web 应用程序,在实现分页时遇到了问题。

适合我的 Controller 方法:

@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;
}

以及 View :

<#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>

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

<li class="page-item"><a class="page-link" href="#">3</a></li> 

URI 没有改变,只是页面参数变成了某个号码。感谢任何线索,也包括批评。

最佳答案

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

关于java - Spring分页,如何修改Uri中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com