如何在 Spring MVC 中调用同一控制器的另一个方法内的同一控制器的方法
How to call a method of same controller inside another method of same controller in Spring MVC
我有这个方法映射
@PostMapping("**/filtrarprodutospreco")
public void preprocessamentoprodutos(Pageable pageable, FiltroProdutosDto filtro) {
filtro.setFiltrarPor("1");
filtro.setItensPorPag("12");
filtrarProdutos(pageable, filtro);
}
处理完数据后,我想用第二种方法的参数对象(可分页和过滤器)调用其他方法:
@PostMapping("**/filtrarprodutos")
public ModelAndView filtrarProdutos(Pageable pageable, FiltroProdutosDto filtro) {
ModelAndView model = new ModelAndView("product");
Categoria categoria = categoriaRepository.findById(filtro.getCategoriaId()).get();
if(filtro.getPrecoDe() == null) {
filtro.setPrecoDe(0D);
}
if(filtro.getPrecoAte() == null) {
filtro.setPrecoAte(1000000000D);
}
if(filtro.getFiltrarPor().contains("1")){
model.addObject("produtos", produtoRepository.filtroProdutos(categoria, filtro.getPrecoDe(), filtro.getPrecoAte(), PageRequest.of(0, Integer.parseInt(filtro.getItensPorPag()), Sort.by("nome"))));
}else {
model.addObject("produtos", produtoRepository.filtroProdutos(categoria, filtro.getPrecoDe(), filtro.getPrecoAte(), PageRequest.of(0, Integer.parseInt(filtro.getItensPorPag()), Sort.by("precoNovo"))));
}
model.addObject("id", filtro.getCategoriaId());
model.addObject("categorias", categoriaRepository.findAll());
model.addObject("filtro", filtro);
return model;
}
这第二种方法使用了ModelAndView来重定向页面,所以我想通过第一种方法调用。 Spring 使用对象调用第二个方法,第二个方法将这些对象和 returns 带到在 (ModelAndView model = new ModelAndView("product")) 中配置的页面。
如何让第一个方法调用第二个方法并重定向到视图?
让第一个方法 return 一个字符串和 return 重定向到第二个方法,如下所示:
@PostMapping("**/filtrarprodutospreco")
public String preprocessamentoprodutos(Pageable pageable, FiltroProdutosDto filtro) {
filtro.setFiltrarPor("1");
filtro.setItensPorPag("12");
filtrarProdutos(pageable, filtro);
return "redirect:/filtrarprodutos";
}
我有这个方法映射
@PostMapping("**/filtrarprodutospreco")
public void preprocessamentoprodutos(Pageable pageable, FiltroProdutosDto filtro) {
filtro.setFiltrarPor("1");
filtro.setItensPorPag("12");
filtrarProdutos(pageable, filtro);
}
处理完数据后,我想用第二种方法的参数对象(可分页和过滤器)调用其他方法:
@PostMapping("**/filtrarprodutos")
public ModelAndView filtrarProdutos(Pageable pageable, FiltroProdutosDto filtro) {
ModelAndView model = new ModelAndView("product");
Categoria categoria = categoriaRepository.findById(filtro.getCategoriaId()).get();
if(filtro.getPrecoDe() == null) {
filtro.setPrecoDe(0D);
}
if(filtro.getPrecoAte() == null) {
filtro.setPrecoAte(1000000000D);
}
if(filtro.getFiltrarPor().contains("1")){
model.addObject("produtos", produtoRepository.filtroProdutos(categoria, filtro.getPrecoDe(), filtro.getPrecoAte(), PageRequest.of(0, Integer.parseInt(filtro.getItensPorPag()), Sort.by("nome"))));
}else {
model.addObject("produtos", produtoRepository.filtroProdutos(categoria, filtro.getPrecoDe(), filtro.getPrecoAte(), PageRequest.of(0, Integer.parseInt(filtro.getItensPorPag()), Sort.by("precoNovo"))));
}
model.addObject("id", filtro.getCategoriaId());
model.addObject("categorias", categoriaRepository.findAll());
model.addObject("filtro", filtro);
return model;
}
这第二种方法使用了ModelAndView来重定向页面,所以我想通过第一种方法调用。 Spring 使用对象调用第二个方法,第二个方法将这些对象和 returns 带到在 (ModelAndView model = new ModelAndView("product")) 中配置的页面。 如何让第一个方法调用第二个方法并重定向到视图?
让第一个方法 return 一个字符串和 return 重定向到第二个方法,如下所示:
@PostMapping("**/filtrarprodutospreco")
public String preprocessamentoprodutos(Pageable pageable, FiltroProdutosDto filtro) {
filtro.setFiltrarPor("1");
filtro.setItensPorPag("12");
filtrarProdutos(pageable, filtro);
return "redirect:/filtrarprodutos";
}