I am using Spring 3.1's new support for Flash Attribute to set the flash attributes in the RedirectAttributes object in the controller and then call the redirect. This redirect request, in turn, gets into the filter, which then sends it along its fun path to the JSP for which it is intended. Problem: I do not see flash attributes from the doFilter() filter method or from JSP. Attributes without flash memory (URL) make it just perfect.
The controller that performs the redirection:
@RequestMapping("/pages/login") public String login (HttpServletRequest request, Map<String, Object> model, RedirectAttributes redirectAttributes) { model.put("userId", "batman"); String redirectUrl = request.getParameter("redirectUrl"); if (redirectUrl != null) { redirectAttributes.addAttribute("attr1","ababab"); redirectAttributes.addFlashAttribute("flashAttr1", "flashflash"); for (Iterator<String> iterator = model.keySet().iterator(); iterator.hasNext();) { String key = iterator.next(); redirectAttributes.addFlashAttribute(key, model.get(key)); } return "redirect:"+redirectUrl; } else { return "pages/login"; } }
A filter that receives a redirect does nothing interesting in this case:
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; //if (httpRequest.getSession().getAttribute("userId") == null) { //...do some stuff here which invokes controller above as well as the redirect //} else { chain.doFilter(request, response); //} }
The page that is redirected to the following filter:
... <title>Test Web App 1</title> </head> <body> <p>Flash attribute: <c:out value="${flashAttr1}"/></p> <p>Welcome <c:out value="${userId}"/>!</p> </body> </html>
Neither flashAttr1 nor userId is populated on the page. The attr1 non-flash attribute set by the controller is displayed in the page URL parameters, so it works.
Here are some results from log4j after I installed springfamework.web in DEBUG:
19:15:44,406 DEBUG http-8080-1 view.ContentNegotiatingViewResolver:494 - Returni ng redirect view [org.springframework.web.servlet.view.RedirectView: name 'redir ect:http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp'; URL [http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp]] 19:15:44,406 DEBUG http-8080-1 servlet.DispatcherServlet:1155 - Rendering view [org.springframework.web.servlet.view.RedirectView: name 'redirect:http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp'; URL [http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp]] in DispatcherServlet with name 'dispatcher' 19:15:44,421 DEBUG http-8080-1 support.DefaultFlashMapManager:199 - Saving Flash Map=[Attributes={userId=batman, flashAttr1=flashflash}, targetRequestPath=/test- webapp-1/protected/protected_page.jsp, targetRequestParams={attr1=[ababab]}] 19:15:44,421 DEBUG http-8080-1 servlet.DispatcherServlet:913 - Successfully comp leted request
After briefly stopping the filter shown above, I got to the page with the URL
http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp?attr1=ababab
But none of the attributes that I expect to find a JSP is displayed. I also debugged the doFilter() method shown above and could not find the flash attributes in the request session.
I am not sure exactly what is wrong at this moment. Everything works as expected, with the exception of these flash attributes. If there is anything else that I must provide in order to make the situation clearer, I will be glad.