EL表达式基本概念
EL表达式,全称是Expression Language。意为表达式语言。
作用是用于在JSP页面中获取数据,
让JSP脱离java代码块和JSP表达式。
基本语法
EL表达式的语法格式非常简单,
**${表达式内容} **
EL表达式
<font color='red'>`${message}`</font>
JSP表达式
`<%=request.getAttribute("message")%>`
More ActionsJava代码块
`<%
<br/> <br/>
String message = (String)request.getAttribute("message");<br/>
out.write(message);<br/>
%>`
EL表达式基本用法
易错点:
el表达式里面的 ${ } 这个括号内写的是
pageContext.setAttribute(“stu”,stu);他的第一个参数,也就是将转入
到页面共享域数据 起的名字,页面共享域是在页面作为存放传入数据的一个容器,这个容器 是将后端代码的数据通过 域对象的 放进去,前端代码要获取后端代码的数据是 ,就通过 stu 这个名字 找到要获取的具体数据。
<%--1.向域对象中添加数据--%>
<% request.setAttribute("username","zhangsan"); %>
<%--2.获取数据--%>
Java代码块:<% out.println(request.getAttribute("username")); %> <br>
JSP表达式:<%= request.getAttribute("username")%> <br>
EL表达式:${username}
<%--1.获取基本数据类型--%>
<% pageContext.setAttribute("num",10); %>
基本数据类型:${num} <br>
<%--2.获取自定义对象类型--%>
<%
Student stu = new Student("张三",23);
stu = null;
pageContext.setAttribute("stu",stu);
%>
<%--EL表达式中没有空指针异常--%>
自定义对象:${stu} <br>
<%--stu.name 实现原理 getName()--%>
学生姓名:${stu.name} <br>
学生年龄:${stu.age} <br>
<%--3.获取数组类型--%>
<%
String[] arr = {"hello","world"};
pageContext.setAttribute("arr",arr);
%>
数组:${arr} <br>
0索引元素:${arr[0]} <br>
1索引元素:${arr[1]} <br>
<%--EL表达式中没有索引越界异常--%>
8索引元素:${arr[8]} <br>
<%--EL表达式中没有字符串拼接--%>
0索引拼接1索引的元素:${arr[0]} + ${arr[1]} <br>
<%--4.获取List集合--%>
<%
ArrayList<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
pageContext.setAttribute("list",list);
%>
List集合:${list} <br>
0索引元素:${list[0]} <br>
<%--5.获取Map集合--%>
<%
HashMap<String,Student> map = new HashMap<>();
map.put("hm01",new Student("张三",23));
map.put("hm02",new Student("李四",24));
pageContext.setAttribute("map",map);
%>
Map集合:${map} <br>
第一个学生对象:${map.hm01} <br>
第一个学生对象的姓名:${map.hm01.name}
<%--empty 是空的 true 不是空的 false
1. 可以判断对象是否为空
2. 可以判断字符串是否为空字符串
3. 可以判断集合长度是否为0
--%>
<%
String s = new String("洛天依");
String a = "sssss";
ArrayList list = new ArrayList<>();
list.add("vvvvvv");
pageContext.setAttribute("s",s);
pageContext.setAttribute("a",a);
pageContext.setAttribute("list",list);
%>
<%--三元运算符 根据性别的数据来让性别单选框默认被选中--%>
<input type="radio" name="gender" value="男" ${gender=="men"?"checked":""}>男
<input type="radio" name="gender" value="女" ${gender=="women"?"checked":""}>女
获取四大域中的数据
它只能从四大域中获取数据,调用的就是findAttribute(name,value);
方法,根据名称由小到大逐个域中查找,找到就返回,找不到就什么都不显示。
它可以获取对象,可以是对象中关联其他对象,可以是一个List集合,也可以是一个Map集合。具体代码如下:
<%--获取四大域对象中的数据--%>
<%
pageContext.setAttribute("a","洛天依");
request.setAttribute("b","马来");
session.setAttribute("c","房子");
application.setAttribute("d","奔驰");
%>
${a}<br>
${b}<br>
${c}<br>
${d}<br>
<%--获取JSP中其他八个隐式对象 获取虚拟目录名称--%>
<%= request.getContextPath()%>
${pageContext.request.contextPath}
<%= request.getContextPath()%>
${pageContext.request.contextPath}
这两个在页面显示的是他的虚拟目录的名字
EL表达式的11个隐式对象
EL表达式隐式对象,可以让不声明直接来使用
<%--pageContext对象 可以获取其他三个域对象和JSP中八个隐式对象--%>
${pageContext.request.contextPath} <br>
<%--applicationScope sessionScope requestScope pageScope 操作四大域对象中的数据--%>
<% request.setAttribute("username","zhangsan"); %>
${username} <br>
${requestScope.username} <br>
<%--header headerValues 获取请求头数据--%>
${header["connection"]} <br>
${headerValues["connection"][0]} <br>
<%--param paramValues 获取请求参数数据--%>
${param.username} <br>
${paramValues.hobby[0]} <br>
${paramValues.hobby[1]} <br>
<%--initParam 获取全局配置参数--%>
${initParam["pname"]} <br>
<%--cookie 获取cookie信息--%>
${cookie} <br> <%--获取Map集合--%>
${cookie.JSESSIONID} <br> <%--获取map集合中第二个元素--%>
${cookie.JSESSIONID.name} <br> <%--获取cookie对象的名称--%>
${cookie.JSESSIONID.value} <%--获取cookie对象的值--%>
EL中的隐式对象 | 类型 | 对应JSP隐式对象 | 备注 |
---|---|---|---|
PageContext | Javax.serlvet.jsp.PageContext | PageContext | 完全一样 |
ApplicationScope | Java.util.Map | 没有 | 应用层范围 |
SessionScope | Java.util.Map | 没有 | 会话范围 |
RequestScope | Java.util.Map | 没有 | 请求范围 |
PageScope | Java.util.Map | 没有 | 页面层范围 |
Header | Java.util.Map | 没有 | 请求消息头key,值是value(一个) |
HeaderValues | Java.util.Map | 没有 | 请求消息头key,值是数组(一个头多个值) |
Param | Java.util.Map | 没有 | 请求参数key,值是value(一个) |
ParamValues | Java.util.Map | 没有 | 请求参数key,值是数组(一个名称多个值) |
InitParam | Java.util.Map | 没有 | 全局参数,key是参数名称,value是参数值 |
Cookie | Java.util.Map | 没有 | Key是cookie的名称,value是cookie对象 |