Spring Boot Sping Boot -重定向到不同的控制器方法

jljoyd4f  于 2024-01-06  发布在  Spring
关注(0)|答案(5)|浏览(193)

我正在使用SpringBoot和Thymeleaf创建一个非常基本的应用程序。在控制器中,我有2个方法,如下所示:
方法1-此方法显示数据库中的所有数据:

  1. @RequestMapping("/showData")
  2. public String showData(Model model)
  3. {
  4. model.addAttribute("Data", dataRepo.findAll());
  5. return "show_data";
  6. }

字符串
方法2-此方法将数据添加到数据库:

  1. @RequestMapping(value = "/addData", method = RequestMethod.POST)
  2. public String addData(@Valid Data data, BindingResult bindingResult, Model model) {
  3. if (bindingResult.hasErrors()) {
  4. return "add_data";
  5. }
  6. model.addAttribute("data", data);
  7. investmentTypeRepo.save(data);
  8. return "add_data.html";
  9. }


HTML文件对应于这些方法,即show_data. html和add_data. html。
一旦addData方法完成,我想显示数据库中的所有数据。然而,上面的代码重定向到静态的add_data.html页面,新添加的数据不会显示。我需要以某种方式调用控制器上的showData方法,所以我需要将用户重定向到/showData URL。这可能吗?如果可能,如何做到这一点?

3xiyfsfu

3xiyfsfu1#

试试这个:

  1. @RequestMapping(value = "/addData", method = RequestMethod.POST)
  2. public String addData(@Valid Data data, BindingResult bindingResult, Model model) {
  3. //your code
  4. return "redirect:/showData";
  5. }

字符串

92vpleto

92vpleto2#

sparrow的解决方案对我不起作用,它只是呈现文本“redirect:/”
我可以通过将HttpServletResponse httpResponse添加到控制器方法头来使它工作。
然后在代码中,将httpResponse.sendRedirect("/");添加到方法中。
范例:

  1. @RequestMapping("/test")
  2. public String test(@RequestParam("testValue") String testValue, HttpServletResponse httpResponse) throws Exception {
  3. if(testValue == null) {
  4. httpResponse.sendRedirect("/");
  5. return null;
  6. }
  7. return "<h1>success: " + testValue + "</h1>";
  8. }

字符串

rdrgkggo

rdrgkggo3#

下面的解决方案对我来说很有效。getAllCategory()方法显示数据,并将数据添加到数据库中。使用return“redirect:categories”;,将重定向到getAllCategory()方法。

  1. @GetMapping("/categories")
  2. public String getAllCategory(Model model) {
  3. model.addAttribute("categories",categoryRepo.findAll());
  4. return "index";
  5. }
  6. @PostMapping("/categories")
  7. public String createCategory(@Valid Category category) {
  8. categoryRepo.save(category);
  9. return "redirect:categories";
  10. }

字符串
或者使用ajQuery也是可能的。

展开查看全部
yquaqz18

yquaqz184#

你应该从addData请求中返回一个http状态码3xx,并将redirct url放在响应中。

atmip9wb

atmip9wb5#

示例如何重定向
1./ ->/swagger-ui/index. html
1./swagger ->/swagger-ui/index. html

  1. import jakarta.servlet.http.HttpServletResponse;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.ResponseBody;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.io.IOException;
  6. @RestController
  7. public class RedirectController {
  8. private static final String SWAGGER_URL = "/swagger-ui/index.html";
  9. @GetMapping("/")
  10. @ResponseBody
  11. public void root(HttpServletResponse httpResponse) throws IOException {
  12. httpResponse.sendRedirect(SWAGGER_URL);
  13. }
  14. @GetMapping("/swagger")
  15. @ResponseBody
  16. public void swagger(HttpServletResponse httpResponse) throws IOException {
  17. httpResponse.sendRedirect(SWAGGER_URL);
  18. }
  19. }

字符串

展开查看全部

相关问题