首页 文章

Spring MVC中的分页和过滤

提问于
浏览
1

我有一个表单,用户可以在其中过滤待处理的事务 . 我想使用Spring的分页功能 . 只有在我不想过滤时(与getAll()查询相同),这才能正常工作(如我所愿) . 我的问题是,如果我发帖的形式向控制器,它搜索过滤交易,提供有关的页数,总ECT的信息 . 但是,如果我点击分页按钮,它关系到其签名是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";
  }
}

1 回答

  • 0

    使用 @SessionAttributes 将过滤器存储在会话中,因此它可用于分页请求 .

    我前段时间为displaytag和JDAL写了一个例子,但是控制器是一个Spring控制器,所以它对你有用 .

    http://www.jdal.org/doc/displaytag.php

相关问题