el表达式:
目的:
获取作用域中的共享数据.
<%=pageContext.findAttribute("msg") %>
按照从小到大的范围依次从page,request,session,application寻找指定属性名的属性值.
需求:若有值就显示,没有值显示空字符串.
<%=pageContext.findAttribute("msg")!=null?pageContext.findAttribute("msg"):""%>.
等价于:
语法: ${属性名称}--->${msg}.
设置不同作用域的共享数据
pageCotext.setAttribute("msg","pageContectValue");
request.setAttribute("msg","requestContectValue");
session.setAttribute("msg","sessionContectValue");
application.setAttribute("msg","applicationContectValue");
获取不同作用域中,同属性名的属性值
pageCotext:${pageCotext.msg}<br/>
request:{requestScope.msg}<br/>
session:{sessionScope.msg}<br/>
application:{applicationScope.msg}<br/>
![](https://img.haomeiwen.com/i5615911/44f71925b12df0ee.png)
属性找不到异常
javax.el.PropertyNotFoundException:Property 'name' not readable on type cn.itsource._02el.User
在User整个类型中,找不到name属性的getter方法.
在EL中访问JavaBean属性方式:
方式1:使用"."来访问.
方式2:使用 "[]"来访问.
${对象.属性名} ---> 对象.getXxxx,注意属性必须提供getter方法.
若操作的是Map:${对象.key}
比如:${u.name}等价于${u["username"]}
EL其他细节:
获取上下文路径:
Servlet:request.getContextPath();
pageContext.getRequest().getContextPath().
JSP:${pageContext.request.contextPath}
从Tomcat7开始,EL中不仅支持属性访问,还支持调用方法.
${pageContext.getRequest().getContextPath()}
<%
pageContext.setAttribute("s1",new String("ABC"));
pageContext.setAttribute("s2",new String("ABC"));
List list = new ArrayList();
list.add(1);
pageContext.setAttribute("xx",list);
%>
${empty xx} false
${s1 == s2} true
${s1 eq s1} true
${s1 == s2} true
网友评论