JSP隱含變數和Spring中Model在EL表示式中的讀取順序
阿新 • • 發佈:2018-12-30
結論:
1.jsp中用EL表示式(${attribute_name})的讀取順序:model > session > application > requset
2.慎重使用session變數,這個範圍太大了,如果因為一個模組把變數存在session中實現起來更容易,就這樣做了,那麼這就很容易為後面埋下bug.
測試程式碼:
controller類:
package com.my.innerParam.test; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * 測試幾個內建變數中在EL讀取的順序 * @author rocky * */ @Controller public class InnerParam { @RequestMapping("/innerParamTest") public String innerParamTest(HttpServletRequest request,HttpServletResponse response,Model model) { HttpSession session = request.getSession(); session.setAttribute("session", "session223"); ServletContext application = request.getServletContext(); // application.setInitParameter("appicationParam", "appicationParam"); //Initialization parameters can not be set after the context has been initialized application.setAttribute("appicationAttr", "applicationAttr"); request.setAttribute("request", "request"); model.addAttribute("model", "model123"); return "/testParam/testParam1"; } //當變數名字相同時判斷EL表示式用哪一個 @RequestMapping("/innerParamTest2") public String innerParamTest2(HttpServletRequest request,HttpServletResponse response,Model model) { HttpSession session = request.getSession(); session.setAttribute("test", "session223"); ServletContext application = request.getServletContext(); // application.setInitParameter("appicationParam", "appicationParam"); //Initialization parameters can not be set after the context has been initialized application.setAttribute("test", "applicationAttr"); request.setAttribute("test", "request"); model.addAttribute("test", "model123"); return "/testParam/testParam2"; } //當model是null的時候 @RequestMapping("/innerParamTest3") public String innerParamTest3(HttpServletRequest request,HttpServletResponse response,Model model) { HttpSession session = request.getSession(); session.setAttribute("test", "session223"); ServletContext application = request.getServletContext(); // application.setInitParameter("appicationParam", "appicationParam"); //Initialization parameters can not be set after the context has been initialized application.setAttribute("test", "applicationAttr"); request.setAttribute("test", "request"); model.addAttribute("test", null); return "/testParam/testParam3"; } }
jsp頁面:
1、顯示
<h3>session : ${session}</h3> <h3>appicationAttr : '${appicationAttr}'</h3> <h3>appicationParam : '${appicationParam}'</h3> <h3>request : '${request}'</h3> <h3>model : '${model}'</h3> 執行結果 session : session223 appicationAttr : 'applicationAttr' appicationParam : '' request : 'request' model : 'model123'
2、變數名字相同時
<h3>session : ${test}</h3> <h3>appicationAttr : '${test}'</h3> <h3>appicationParam : '${test}'</h3> <h3>request : '${test}'</h3> <h3>model : '${test}'</h3> 執行結果 session : model123 appicationAttr : 'model123' appicationParam : 'model123' request : 'model123' model : 'model123'