原文链接:http://www.cnblogs.com/rocky-AGE-24/p/5816193.html
结论:
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'