EL表达式
Expression Language表达式语言,代替JSP页面中数据访问时的复杂编码,可以⾮常便捷地取出域对象(pageContext、request、session、application)中保存的数据,前提是⼀定要先 setAttribute,EL 就相当于在简化 getAttribute。
${变量名} 变量名就是 setAttribute 对应的 key 值。
<%
String name=(String)request.getAttribute("name");
%>
<%=name%>
<hr/>
//EL表达式与上述一样,可以取出name值
${name}
1、EL 对于 4 种域对象的默认查找顺序:(优先级pageContext最高)
pageContext -》request-〉session-》application
按照上述的顺序进⾏查找,找到⽴即返回,在 application 中也⽆法找到,则返回 null。
2、指定作⽤域进⾏查找
pageContext:
p
a
g
e
S
c
o
p
e
.
n
a
m
e
r
e
q
u
e
s
t
:
{pageScope.name} request:
pageScope.namerequest:{requestScope.name}
session:
s
e
s
s
i
o
n
S
c
o
p
e
.
n
a
m
e
a
p
p
l
i
c
a
t
i
o
n
:
{sessionScope.name} application:
sessionScope.nameapplication:{applicationScope.name}
数据级联:
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>成绩</th>
<th>地址</th>
</tr>
<tr>
<%-- 注意:el表达式通过下面的方式获取,user.id不是通过user的id来获取值,而是通过getID()方法。先将id变为Id,再在前面加上get--%>
<th>${user.id}</th>
<%-- <%--%>
<%-- ((User) pageContext.getAttribute("user")).getId();--%>
<%-- %>--%>
<td>${user.name}</td>
<td>${user.score}</td>
<td>${user.address.value}</td>
</tr>
</table>
package com.y.entity;
public class Address {
private Integer id;
private String value;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Address(Integer id, String value) {
this.id = id;
this.value = value;
}
}
package com.y.entity;
public class User {
private Integer id;
private String name;
private Double score;
private Address address;
public User(Integer id, String name, Double score) {
this.id = id;
this.name = name;
this.score = score;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
//重写
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public User(Integer id, String name, Double score, Address address) {
this.id = id;
this.name = name;
this.score = score;
this.address = address;
}
}
EL 执⾏表达式
&& || ! < > <= >=.
${num1&&num2}
&& || ! < > <= <= ==
&& and
|| or
! not
== eq
!= ne
< lt
gt
<= le
= ge
empty 变量为 null,⻓度为0的String,size为0的集合