Spring 3.1 has added new feature called flash attributes. This is one of the most wanted feature from the spring developers. This solves the problem occured when POST/Redirect/Get pattern in the request. When user makes a request to the server via POST method, once the process is completed, the requested resourced will be served through either forward/redirect to the appropriate view. The problem with the forward is, when the user does F5 or Refresh the same page, the POST data is re-submitted because the request is same.
When we are using redirect, the forms are not re-submitted and only the final view is displayed. The real problem here is, when we invoke redirect, it loses all the request values which would be useful in the subsequent request. The developers used some workaround or @SessionAttributes to store the values. With the release of Spring 3.1, flash attributes solve that issue. Spring container stores the variables temporarily and destroys them after the request.
Are looking for the Spring beginner tutorials? Read our Getting Started Spring Tutorials.
FlashAttributesController.java
package javabeat.net; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller public class FlashAttributesController { @RequestMapping(value="/flashexample") public String getResult(@ModelAttribute("user") UserDetails userDetails, final RedirectAttributes redirectAttributes){ //Some logics goes here. //Before redirect set some values to Flash attributes redirectAttributes.addFlashAttribute("user", userDetails); redirectAttributes.addFlashAttribute("message", "Welcome to test page!!!"); return "redirect:/flashredirect"; } @RequestMapping(value="/flashredirect") public String getResultRedirect(@ModelAttribute UserDetails userDetails ){ return "example"; } @ModelAttribute("user") public UserDetails getUser(@RequestParam String userName){ UserDetails details = new UserDetails(); details.setUserName(userName); return details; } }
example.jsp
<html> <body> <h1>${message} for user ${user.userName}</h1> </body> </html>
- Add the RedirectAttributes in the controller method arguments.
- redirectAttributes.addFlashAttribute for adding any number of variables in the scope. Note that, this values are persisted for the very short time. Till the next request. we are not in the controll of where the variables are stored.
- While returning the view, use “redirect” for making the new requtest which is also defined in the same controller class.
- I have define @ModelAttribure for storing the values to a model object. The values are coming from the request parameter. This method is always invoked bfore the controller method.
- The above jsp file simply prints the message which is stored in the flash attributes.
- The above example is only for demonstrating the power of flash attributes, in real time the above example may not make sense because I have used model attributes to store the values, etc. only for making this example to run.
The spring configuration file is very much similar to any other examples used for the spring mvc application. However, don’t forget to add the following line of snippet in the configuration file.
<mvc:annotation-driven />
If you miss the above line, you may see the error like one below.
nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String javabeat.net.FlashAttributesController. getResult(javabeat.net.UserDetails,org.springframework.web.servlet.mvc.support. RedirectAttributes)]; nested exception is java.lang.IllegalStateException: Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure classes to use this argument.
If you run the application and access the web page using this URL:
http://localhost:8080/SpringExamples/flashexample?userName=TestUser
It will be reditec to this page:
http://localhost:8080/SpringExamples/flashredirect
If you have any questions on using this feature, please write a comment.