EL 全名为Expression Language,用来替代<%= %>脚本表达式。
基本结构为${表达式}。
<h1>获取常量h1> ${123} ${123.32} ${"abc"} ${true} |
el会自动从四大作用域中搜寻域属性来使用
如果找不到什么都不输出
<h1>获取变量h1> <% pageContext.setAttribute("name","zs"); request.setAttribute("name","ls"); %> ${name} |
<h1>获取数组中的数据h1> <% String [] addrs = new String[]{"bj","sh","gz"}; pageContext.setAttribute("addrs",addrs); %> ${addrs[1]} |
<h1>获取集合中的数据h1> <% List list.add("刘备"); list.add("关羽"); list.add("张飞"); pageContext.setAttribute("list",list); %> ${list[0]} |
<h1>获取Map中的数据h1> <% Map map.put("name","诸葛亮"); map.put("age","25"); map.put("wife","黄月英"); map.put("job",new String[]{"军师","法师","中单","dps"}); pageContext.setAttribute("map",map); %> ${map["name"]} ${map["age"]} ${map["wife"]} ${map["job"][0]} |
<h1>获取javabean的属性h1> <% User user = new User(1,"zs",18,"bj"); pageContext.setAttribute("user",user); %> ${user.id} ${user.name} ${user.age} ${user.addr} |
<h1>算数运算h1> ${2+3} ${2+"3"} |
![]() |
<h1>关系运算h1> ${3>2} ${3 gt 2} ${3 < 1} ${3 lt 1} |
![]() |
<h1>逻辑运算h1> ${3>2 && 3<1} ${3>2 || 3<1} ${!(3>2 || 3<1)} |
判断对象是否为null
判断字符串是否为空串
判断数组、集合、map是否没有任何元素
<h1>empty运算h1> <% pageContext.setAttribute("user",null); pageContext.setAttribute("addr",""); pageContext.setAttribute("list",newArrayList()); %> ${emptyuser} ${emptyaddr} ${emptylist} |
<h1>三元运算符h1> <% pageContext.setAttribute("age",20); %> ${age>18?"yes~":"no~"} |
在el表达式中,共有11个内置对象,可以直接在el中使用
pageContext |
通过此内置对象,可以在el中获取其他八大隐式对象
<h1>pageContexth1> ${pageContext.request.contextPath} ${pageContext.session.id} |
pageScope | pageContext域 |
requestScope | request域 |
sessionScope | session域 |
applicationScope | ServletContext域 |
<h1>代表四大作用域的对象h1> <% pageContext.setAttribute("name","zl"); request.setAttribute("name","zs"); session.setAttribute("name","ls"); application.setAttribute("name","ww"); %> ${requestScope.name} ${sessionScope.name} ${applicationScope.name} ${pageScope.name} |
param | Map |
paramValues | Map |
<h1>代表请求参数值的对象h1> ${param.username} ${param.age} ${paramValues.like[0]} ${paramValues.like[1]} ${paramValues.like[2]} |
header | Map |
headerValues | Map |
<h1>代表请求头的对象h1> ${header.Cookie} ${header["Accept-Encoding"]} |
cookie | 请求中所有cookie对象组成的Map |
<h1>代表Cookie对象h1> ${cookie.JSESSIONID.value} |
initParam | 所有应用初始化参数组成的Map |
<h1>代表应用初始化参数的对象h1> ${initParam.ck1} ${initParam.ck2} |
上一篇:【Linux】信号