我有一个表单,用户可以在其中过滤待处理的事务。我想使用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";
}
有没有解决方案,如何一起实现过滤和分页?
更新:感谢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";
}
}