How it works...

Redirection is essential in designing the navigation paths of a system. One page can jump from another through form transactions and hyperlinks. Internally, Spring has two ways of implementing redirection. The old way is to use RedirectView in a handler method. Once a method returns RedirectView, it technically invokes HttpServletResponse.sendRedirection(), which performs the actual navigation process. The modern implementation is through the use of the redirect keyword, which requires a handler to return only the ModelAndView object for passing request objects and executing the views. In short, there is no longer a built-in Flash-scoped object but an improvised one. Thus, it is a requirement that the redirected handler uses @ModelAttribute to catch those improvised Flash-scoped objects.

There is always one problem when implementing navigation and that is the passing of objects from the originator to the redirected page. Request-scoped objects will give us NULL since they are non-existent already after the request dispatch. The redirection always happens after the request dispatch; otherwise, if it interrupts the request-response handshake, IllegalStateException will be thrown. Using RedirectView, we can use RedirectAttributes to generate and store Flash-scoped objects which can persist during navigations. The RedirectAttributes has an addFlashAttribute() method for Flash-scoped objects and addAttribute() for the usual request-scoped attributes.

On the other hand, using the modern way of implementation requires us to use the workaround of ModelAndView to pass all objects during redirection and @ModelAttribute for the counterpart handler to fetch all this data.

Session handling is not the ultimate solution in sharing objects from one page to another. Sessions can slow down runtime performance, especially if they are loaded with huge objects such as collections of JDBC objects for their entire production. Creating or simulating Flash-scoped is the recommended solution whenever there are navigations all over the software specification.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.70