Junior — Middle
At what level of processing does redirection occur after calling DispatcherServlet in an MVC application?
sobes.tech AI
Answer from AI
Redirection (redirect) in Spring MVC occurs after the request is processed by the controller, that is, at the controller level or after it. The DispatcherServlet first receives the request, determines which controller should handle it, calls the appropriate controller method, and within the controller or in the returned ModelAndView object, it indicates what needs to be redirected.
For example, in a controller, you can return a string with the prefix "redirect:", which instructs the DispatcherServlet to perform an HTTP redirect:
@GetMapping("/old-url")
public String redirect() {
return "redirect:/new-url";
}
Thus, the redirection occurs at the controller level, after the DispatcherServlet call, but before the final response is sent to the client.