Spring Boot - redirect to another controller method

I am very new to Spring Boot. I am creating a very basic application with SpringBoot and Thymeleaf. In the controller, I have 2 methods:

Method1 - this method displays all the data from the database:

  @RequestMapping("/showData")
public String showData(Model model)
{
    model.addAttribute("Data", dataRepo.findAll());
    return "show_data";
}

Method2 - this method adds data to the database:

@RequestMapping(value = "/addData", method = RequestMethod.POST)
public String addData(@Valid Data data, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        return "add_data";
    }
    model.addAttribute("data", data);
    investmentTypeRepo.save(data);

    return "add_data.html";
}

HTML files corresponding to these methods are presented, i.e. show_data.html and add_data.html.

As soon as the addData method completes, I want to display all the data from the database. However, the above redirects the code to the static add_data.html page and the newly added data is not displayed. I need to somehow call the showData method on the controller, so I need to redirect the user to the URL / showData. Is it possible? If so, how can this be done?

Thanks in advance.

+4
2

,

 @RequestMapping(value = "/addData", method = RequestMethod.POST)
    public String addData(@Valid Data data, BindingResult bindingResult, Model model) {

     //your code

     return "redirect:/showData";
}
+14

http 3xx addData redirct url .

0

Source: https://habr.com/ru/post/1662363/


All Articles