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,結束

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章