springmvc重定向后jsp如何获取其中的flashAttribute?
大家都知道我们在springmvc3.1以后可以利用RedirectAttributes在action重定向后来进行数据传递,来解决困扰大家许久的action之间跳转数据丢失的问题。
那么我们如何在action重定向到jsp页面后通过EL表达式获取其中的参数呢?
当然你也可以使用attribute来进行页面数据的传递但是会拼接到url中,直接显示给用户,会有一定的安全问题。
测试代码:
TestController.java
- @RequestMapping(value = "/test", method = {RequestMethod.GET})
- public String ajaxSubmit(HttpServletRequest request, HttpServletResponse response,
- final RedirectAttributes directAttributes)
- {
- // FlashAttribute页面重定向后数据存储在FlashMap中
- directAttributes.addFlashAttribute("test", "0001");
- // Attribute页面重定向后数据拼接到url后
- directAttributes.addAttribute("test1", "0002");
- return "redirect:/index.jsp";
- }
index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
- <%
- String path = request.getContextPath();
- String basePath =
- request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
- %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <html>
- <body>
- <a href="account/test">TEST</a>
- <br> test: ${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['test']}
- <br> test1: ${param.test1}
- </body>
- </html>
一开始我们使用attribute进行数据传递,但是会拼接到url所以想到使用flash attribute 但是不知道如何获取在页面中,最后debug
才知道如何获取其中的值
取值EL表达式:
- ${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['test']}
ok,结束
更好的解决办法:
http://blog.csdn.net/qq_35448976/article/details/78952148