JSP隐含变量和Spring中Model在EL表达式中的读取顺序

23 篇文章 0 订阅
2 篇文章 0 订阅

原文链接: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'


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值