springmvc重定向后jsp如何获取其中的flashAttribute?

大家都知道我们在springmvc3.1以后可以利用RedirectAttributes在action重定向后来进行数据传递,来解决困扰大家许久的action之间跳转数据丢失的问题。

那么我们如何在action重定向到jsp页面后通过EL表达式获取其中的参数呢?

当然你也可以使用attribute来进行页面数据的传递但是会拼接到url中,直接显示给用户,会有一定的安全问题。

测试代码:


TestController.java

[java] view plain copy
  1. @RequestMapping(value = "/test", method = {RequestMethod.GET})  
  2.    public String ajaxSubmit(HttpServletRequest request, HttpServletResponse response,  
  3.        final RedirectAttributes directAttributes)  
  4.    {  
  5.        // FlashAttribute页面重定向后数据存储在FlashMap中  
  6.        directAttributes.addFlashAttribute("test""0001");  
  7.          
  8.        // Attribute页面重定向后数据拼接到url后  
  9.        directAttributes.addAttribute("test1""0002");  
  10.        return "redirect:/index.jsp";  
  11.    }  


index.jsp

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath =  
  6.         request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";  
  7. %>  
  8. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  9. <html>  
  10. <body>  
  11.     <a href="account/test">TEST</a>  
  12.     <br> test: ${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['test']}  
  13.     <br> test1: ${param.test1}  
  14. </body>  
  15. </html>  


解释:

一开始我们使用attribute进行数据传递,但是会拼接到url所以想到使用flash attribute 但是不知道如何获取在页面中,最后debug


springmvc重定向后jsp如何获取其中的flashAttribute?

才知道如何获取其中的值

取值EL表达式:

[html] view plain copy
  1. ${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['test']}  

ok,结束


更好的解决办法:

http://blog.csdn.net/qq_35448976/article/details/78952148