Spring MVC 3.1中的Flash属性对重定向的JSP不可见



我使用Spring 3.1新的Flash属性支持在控制器中的RedirectAttributes对象上设置Flash属性,然后调用重定向。这个重定向请求又被一个过滤器捕获,然后过滤器将它愉快地发送到它想要的JSP。问题是:无论是在过滤器的doFilter()方法中还是在JSP中,我都看不到flash属性。非flash(URL)属性使它很好。

执行重定向的控制器:

@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";
    }
}

在这种情况下,接收重定向的过滤器不会做任何有趣的事情:

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);
    //}
}

按照过滤器重定向到的页面:

...
<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>

flashAttr1userId最终都不会填充到页面中。控制器设置的attr1非闪存属性确实出现在页面的URL参数中,因此这似乎有效。

以下是我将springfamework.web设置为调试后log4j的一些输出:

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

在我上面显示的过滤器处短暂停留后,我被带到带有URL 的页面

http://my_hostname:8080/test-webapp-1/protected/protected_page.jsp?attr1=ababab

但是,我期望JSP找到的两个属性都没有显示出来。我还通过上面显示的doFilter()方法进行了调试,但未能在请求的会话中找到flash属性。

我不确定这一点到底出了什么问题。除了那些闪光属性外,一切都如预期。如果我还需要提供什么东西来让情况更清楚,我会很乐意的。

几个月前通过AJAX相关重定向遇到了这个问题。如果使用只读HTTPPOST模式,则可以指定@ResponseStatus模拟POST。此外,请确保您的方法返回ViewModelAndView(与String相反),以便Spring知道查找给定@RequestMapping的Flash范围。

伪码:

@RequestMapping(...)
@ResponseStatus(OK)
public ModelAndView login (...) {
    ...
}

最新更新