Is there any difference between
public class Controller1 extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new AnotherController().handleRequest(request, response);
}
}
and
@Controller
public class Controller1 {
@RequestMapping ...
public String handleRequest() {
return "forward:/path_to_my_another_controller";
}
}
A request can be basically processed in three ways: a) resolved by Spring in a controller action, b) forwarded to a different controller action, c) redirected to client to fetch another URL. Forward: performed internally by Spring. the browser is completely unaware of forward, so its original URL remains intact.
Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL. Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.
redirect will respond with a 302 and the new URL in the Location header; the browser/client will then make another request to the new URL. forward happens entirely on a server side. The Servlet container forwards the same request to the target URL; the URL won't change in the browser.
You can use RedirectAttributes to store flash attributes and they will be automatically propagated to the "output" FlashMap of the current request. A RedirectAttributes model is empty when the method is called and is never used unless the method returns a redirect view name or a RedirectView.
By creating controllers yourself, you will prevent Spring from injecting any dependencies into them. This may cause your self-created controllers to not work correctly.
If you really need to chain controllers like this, I would ask the Spring application context for an instance of the controller you want instead of creating one with the new
operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With