I have a form, in which user can filter pending transactions. I want to use the pagination feature of Spring. This works (as I wanted) only if I don't want to filter (same as getAll() query). My problem is if I post the form to the controller, it searches the filtered transactions, gives the information about the number of pages, total ect.. But if I click on the pagination button, it goes to the method which signature is GET (URL is for example localhost.../pending?page=2&size=20), and my form is initialized with default values. So the filter doesn't work, just the pagination.
我有一个表单,用户可以在其中过滤待处理的事务。我想使用Spring的分页功能。只有在我不想过滤时(与getAll()查询相同),这才能正常工作(如我所愿)。我的问题是,如果我将表单发布到控制器,它会搜索已过滤的事务,提供有关页数,总数的信息。但是,如果我单击分页按钮,它将转到签名为GET的方法( URL例如是localhost ... / pending?page = 2&size = 20),我的表单用默认值初始化。所以过滤器不起作用,只是分页。
@RequestMapping(value = "/pending", method = RequestMethod.GET, produces = "text/html")
public String getPendingTransactions(@ModelAttribute("pendingForm") PendingTransactionForm pendingForm, Model uiModel, HttpServletRequest httpServletRequest, Pageable pageable) {
PageWrapper<Transaction> transactionItems = new PageWrapper<Transaction>(transactionService.searchPendingItemsByParams(pendingForm, pageable));
uiModel.addAttribute("pendingForm", pendingForm);
uiModel.addAttribute("transactionItems", transactionItems);
uiModel.addAttribute("shoplist", transactionService.getShopListForNotFinishedItems());
return "transaction/pendingtransactions";
}
@RequestMapping(value = "/pending", method = RequestMethod.POST)
public String processPendingTransactions(@ModelAttribute("pendingForm") PendingTransactionForm pendingForm, Model uiModel, HttpServletRequest httpServletRequest, Pageable pageable) {
PageWrapper<Transaction> transactionItems = new PageWrapper<Transaction>(transactionService.searchPendingItemsByParams(pendingForm, pageable));
uiModel.addAttribute("pendingForm", pendingForm);
uiModel.addAttribute("transactionItems", transactionItems);
uiModel.addAttribute("shoplist", transactionService.getShopListForNotFinishedItems());
return "transaction/pendingtransactions";
}
Is there any solution, how to implement the filter and pagination together?
有没有解决方案,如何一起实现过滤和分页?
UPDATE: thanks to Jose Luis Martin's answer, it works!
更新:感谢Jose Luis Martin的回答,它的确有效!
@RequestMapping("/transactions")
@Controller
@SessionAttributes("pendingForm")
public class TransactionsController {
@ModelAttribute("pendingForm")
public PendingTransactionForm initializePendingForm() {
return new PendingTransactionForm();
}
@RequestMapping(value = "/pending", method = {RequestMethod.POST, RequestMethod.GET}, produces = "text/html")
public String getPendingTransactions(@ModelAttribute("pendingForm") PendingTransactionForm pendingForm, Model uiModel, Pageable pageable) {
PageWrapper<Transaction> transactionItems = new PageWrapper<Transaction>(transactionService.searchPendingItemsByParams(pendingForm, pageable));
uiModel.addAttribute("transactionItems", transactionItems);
uiModel.addAttribute("shoplist", transactionService.getShopListForNotFinishedItems());
return "transaction/pendingtransactions";
}
}
1 个解决方案
#1
Store the filter in session using @SessionAttributes
, so it will be available for pagination requests.
使用@SessionAttributes将过滤器存储在会话中,因此它可用于分页请求。
I wrote a example for displaytag and JDAL some time ago, but the controller is a Spring controller, so it could be useful for you.
我前段时间为displaytag和JDAL写了一个例子,但是控制器是一个Spring控制器,所以它对你有用。
#1
Store the filter in session using @SessionAttributes
, so it will be available for pagination requests.
使用@SessionAttributes将过滤器存储在会话中,因此它可用于分页请求。
I wrote a example for displaytag and JDAL some time ago, but the controller is a Spring controller, so it could be useful for you.
我前段时间为displaytag和JDAL写了一个例子,但是控制器是一个Spring控制器,所以它对你有用。