Spring Boot - redirect to a different controller method
Asked Answered
L

5

33

I am creating a very basic application with SpringBoot and Thymeleaf. In the controller I have 2 methods as follows:

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 are present corresponding to these methods i.e. show_data.html and add_data.html.

Once 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 invoke the showData method on the controller so I need to redirect the user to the /showData URL. Is this possible? If so, how can this be done?

Luteous answered 30/11, 2016 at 5:11 Comment(0)
P
51

Try this:

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

    //your code

    return "redirect:/showData";
}
Penna answered 30/11, 2016 at 6:1 Comment(2)
This will not work if you are trying to redirect request From one RestController to another RestControllerFastidious
This makes no sense to meReceivable
P
19

sparrow's solution did not work for me. It just rendered the text "redirect:/"

I was able to get it working by adding HttpServletResponse httpResponse to the controller method header.

Then in the code, adding httpResponse.sendRedirect("/"); into the method.

Example:

@RequestMapping("/test")
public String test(@RequestParam("testValue") String testValue, HttpServletResponse httpResponse) throws Exception {
    if(testValue == null) {
        httpResponse.sendRedirect("/");
        return null;
    }
    return "<h1>success: " + testValue + "</h1>";
}
Pituitary answered 24/5, 2018 at 15:15 Comment(1)
If it just rendered the text "redirect:/" you probably had your controller annotated as @RestController, when you need only @Controller annotation for it to work.Mulloy
R
1

Below Solution worked for me. getAllCategory() method displays the data and createCategory() method add data to the database. Using return "redirect:categories";, will redirect to the getAllCategory() method.

@GetMapping("/categories")
public String getAllCategory(Model model) {
    model.addAttribute("categories",categoryRepo.findAll());
    return "index";
}

@PostMapping("/categories")
public String createCategory(@Valid Category category) {

    categoryRepo.save(category);
    return "redirect:categories";
}

OR using ajax jQuery also it is possible.

Refinement answered 25/12, 2019 at 4:49 Comment(0)
U
0

You should return a http status code 3xx from your addData request and put the redirct url in the response.

Unthrone answered 30/11, 2016 at 5:41 Comment(0)
C
0

example how to redirect

  1. / -> /swagger-ui/index.html
  2. /swagger -> /swagger-ui/index.html
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class RedirectController {

    private static final String SWAGGER_URL = "/swagger-ui/index.html";

    @GetMapping("/")
    @ResponseBody
    public void root(HttpServletResponse httpResponse) throws IOException {
        httpResponse.sendRedirect(SWAGGER_URL);
    }

    @GetMapping("/swagger")
    @ResponseBody
    public void swagger(HttpServletResponse httpResponse) throws IOException {
        httpResponse.sendRedirect(SWAGGER_URL);
    }
}
Christyna answered 20/12, 2023 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.