jsp取addFlashAttribute值深入理解
結論:兩種方式
a.如果沒有進行action轉發,在頁面中el需要${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['reportId']}
b.如果進行了action轉發則el直接${reportId}
1.沒有進行action轉發
controller程式碼:
@Controller @RequestMapping("/page/login") public class LoginController { @RequestMapping(value= "/redirect.do") public String doRedirect(RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("userId", "ID001"); redirectAttributes.addFlashAttribute("userName", "mike");
redirectAttributes.addAttribute("flag", "opening"); return "redirect:../public/indexTest.jsp"; } }
jsp
<body> here is indexTest.jsp <br> userId is ${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['userId']}<br>
userName is ${userName}<br> flag is ${flag}
param_flag is ${param.flag} </body>
這樣可以正常輸出 url:http://lolcalhost:8080/project/page/public/indexTest.jsp
here is indexTest.jsp
userId is ID001
userName is
flag is opening
param_flag is opening
在jsp中列印session和request
<%
System.out.println("page session parameter:"); //final HttpSession session = request.getSession(); final Enumeration se = session.getAttributeNames(); while (se.hasMoreElements()) { final String key = (String) se.nextElement(); System.out.println(key + "==" + session.getAttribute(key)); } System.out.println("print redirectpage page request parameter:"); final Enumeration reqEnum = request.getParameterNames(); while (reqEnum.hasMoreElements()) { final String s = (String) reqEnum.nextElement(); System.out.println(s + "==" + request.getParameter(s)); } System.out.println("print redirectpage page request attribute:"); final Enumeration reqEnum2 = request.getAttributeNames(); while (reqEnum2.hasMoreElements()) { final String s = (String) reqEnum2.nextElement(); System.out.println(s + "==" + request.getAttribute(s)); }
%>
console輸出
page session parameter: org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS==[FlashMap [attributes={userId=ID001, userName=mike} , targetRequestPath=/project/page/public/indexTest.jsp, targetRequestParams={flag=[opening]}]] print redirectpage page request parameter: flag==opening print redirectpage page request attribute:
說明:addFlashAttribute方法將引數放入了session中的flashmap中儲存起來了,並且隱藏起來,不在瀏覽器中顯示引數,同時傳大物件也不受瀏覽器限制。而redirectAttributes.addAttribute方法則是將引數放到request域中,而且request.getParameter("org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS")出來的值不是標準的資料組,用JSON.parse(str)執行時會報錯,只能用el取
2.通過action二次轉發
controller
@Controller @RequestMapping("/page/login") public class LoginController { @RequestMapping(value = "/redirect.do") public String doRedirect(RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("userId", "ID001"); redirectAttributes.addFlashAttribute("userName", "mike"); redirectAttributes.addAttribute("flag", "opening"); return "redirect:../public/winOpenSucc.do"; }
@RequestMapping(value = "/winOpenSucc.do") public String redirectPage() { return "../public/indexTest.jsp" } }
jsp頁面不變
輸出 url:http://lolcalhost:8080/project/page/public/winOpenSucc.do?flag=opening
here is indexTest.jsp
userId is
userName is mike
flag is opening