什么是EL表达式,EL表达式的作用?
a)EL表达式作用
EL表达式主要式代替jsp页面中的表达式脚本在jsp页面中进行数据的输出。因为EL表达式在输出数据的时候,要比jsp的表达式脚本要简洁很多。
EL表达式的格式是:${表达式}
EL表达式在输出null值的时候,输出的是空串。jsp表达式脚本输出null字符串
b)EL表达式搜索域数据的顺序
EL表达式主要是在JSP页面中输出数据。
主要是输出域对象中的数据。
当四个域中都有相同的key的是数据的时候,EL表达式会按照四个域的从小到大的顺序去进行搜索,找到就输出。
pageContext(pageContextmpl类):当前jsp页面有效
request (httpServletRequest类):一次请求类有效
session (HttpSession类):一个会话范围有效。会话表示打开浏览器访问服务器,直到关闭浏览器
application (ServletContxt类):整个web工程范围内有效,只要web工程部停止,数据都在
EL表达式的输出各种属性
a)输出复杂的Bean对象
i:需求输出person类中普通属性,数组属性。List集合属性和map集合属性
Person类
public class Student {
//需求输出person类中普通属性,数组属性。List集合属性和map集合属性
private String name;
private String[] phone;
private ArrayList<String> city;
private HashMap<String,String> map;
public Student() {
}
public Student(String name, String[] phone, ArrayList<String> city, HashMap<String, String> map) {
this.name = name;
this.phone = phone;
this.city = city;
this.map = map;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getPhone() {
return phone;
}
public void setPhone(String[] phone) {
this.phone = phone;
}
public ArrayList<String> getCity() {
return city;
}
public void setCity(ArrayList<String> city) {
this.city = city;
}
public HashMap<String, String> getMap() {
return map;
}
public void setMap(HashMap<String, String> map) {
this.map = map;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", phone=" + Arrays.toString(phone) +
", city=" + city +
", map=" + map +
'}';
}
jsp页面
<%
Student student = new Student();
ArrayList<String> strings = new ArrayList<>();
strings.add("中国");
strings.add("日本");
HashMap<String, String> hasmap = new HashMap<>();
hasmap.put("key1","value1");
hasmap.put("key2","value2");
hasmap.put("key3","value3");
student.setMap(hasmap);
student.setName("覃圣航");
student.setPhone(new String[]{"19923047544","16851051"});
student.setCity(strings);
request.setAttribute("student",student);
%>
输出Studnet类中的name:${student.name}<br>
输出Studnet类中String数组:${student.phone[0]}<br>
输出Student类中list集合全部:${student.city}<br>
输出Srudnet类中list集合中的某一个:${student.city[0]}<br>
输出Student类中map集合中的所有:${student.map}<br>
<!--key3是map中的键值-->
输出Srudnet类中map集合中的某一个:${student.map.key3}<br>
在EL表达式中student.属性名(其实找的是他对应的get方法)
EL表达式-------运算符
a)关系运算
b)逻辑表达式
c)算术运算
d)empty运算符
可以判断一个数据是否为空,如果为空,则输出true,不为空输出flase。
一下几种情况为空
- 值为null值的时候,为空
- 值为空串的时候,为空
- 值是objec类型数组,长度为零的时候
- list和map元素个数为零的时候
${empty student}
e)三元运算符
表达式1?值1:值2
f)"."运算符和【】中括号运算符
.点运算,可以输出Bean对象中某个属性的值。
输出Studnet类中的name:${student.name}
[]中括号运算,可以输出有序集合某个元素的值。
输出Srudnet类中list集合中的某一个:${student.city[0]}<br>
并且[]中括号运算,还可以输出map集合中key里包含特殊字符的key的值。
<%
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("a.a.a","qin");
request.setAttribute("hasmap",hashMap);
%>
<!--错误方法,他会将.识别成为.运算符-->
${hasmap.a.a.a}
<!--正确方法使用[]运算符-->
${hasmap["a.a.a"]}
EL表达式的11个隐含对象
a)四个特定特定域中的对象
<%
pageContext.setAttribute("key1","pageContext");
request.setAttribute("key1","request");
session.setAttribute("key1","session");
application.setAttribute("key1","application");
%>
${key1}
<!--没有指定对象,那默认是最小的哪一个-->
${requestScope.key1}
b)pagecontext隐含对象
1、协议${pageContext.request.scheme}<br>
2、服务器ip${pageContext.request.serverName}<br>
3、服务器的端口${pageContext.request.serverPort}<br>
4、服务器的工程路径${pageContext.request.contentType}<br>
5、服务器的请求方法${pageContext.request.method}<br>
6、客户端ip地址${pageContext.request.remoteHost}<br>
7、获取会话id编号${pageContext.session.id}<br>
c)其他隐式对象
输出请求参数username的值:${param.username}
输出请求参数多个username的值: ${paramValues.username[0]}
输出请求与头的信息:${header}
输出请求头的信息中的User-Agent():${header["User-Agent"]}
输出请求头的信息中的User-Agent()若User-Agent有多个值:${headerValues}
<br> 获取cookie的对象${cookie}
<br> 获取param对象(param对象在init-param中配置)${initPara
m}