EL 表达式的全称是:Expression Language。是表达式语言。
EL 表达式的什么作用:EL 表达式主要是代替 jsp 页面中的表达式脚本在 jsp 页面中进行数据的输出。
因为 EL 表达式在输出数据的时候,要比 jsp 的表达式脚本要简洁很多。
<%
request.setAttribute("key","值");
%>
表达式脚本输出 key 的值是:
<%=request.getAttribute("key")==null?"":request.getAttribute("key")%>
EL 表达式输出 key 的值是:${key}
EL 表达式的格式是:${表达式}
EL 表达式在输出 null 值的时候,输出的是空串。jsp 表达式脚本输出 null 值的时候,输出的是 null 字符串。
EL 表达式主要是在 jsp 页面中输出数据。
主要是输出域对象中的数据。
当四个域中都有相同的 key 的数据的时候,EL 表达式会按照四个域的从小到大的顺序去进行搜索,找到就输出。
<%
// 往四个域中都保存了相同的 key 的数据。
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
pageContext.setAttribute("key", "pageContext");
%>
${ key }
注释掉pageContext:
四个域的大小:pageContext
按照从小到大的顺序进行搜索然后输出。
例:输出 Person 类中普通属性,数组属性,list 集合属性和 map 集合属性。
Person 类:
public class Person {
// Person 类中普通属性,数组属性。 list 集合属性和 map 集合属性。
private String name;
private String[] phones;
private List cities;
private Map map;
}
jsp输出代码:
<%
Person person = new Person();
person.setName("愷龍");
person.setPhones(new String[]{"18755556666","18688886666","18699998888"});
List cities = new ArrayList();
cities.add(" 大连");
cities.add(" 沈阳");
cities.add(" 北京");
person.setCities(cities);
Map map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
person.setMap(map);
pageContext.setAttribute("p", person);
%>
输出 Person:${ p }
输出 Person 的 name 属性:${p.name}
输出 Person 的 phones 数组属性值:${p.phones[2]}
输出 Person 的 cities 集合中的元素值:${p.cities}
输出 Person 的 List 集合中个别元素值:${p.cities[2]}
输出 Person 的 Map 集合: ${p.map}
输出 Person 的 Map 集合中某个 key 的值: ${p.map.key3}
输出 Person 的 age 属性:${p.age}
结果:
语法:${ 运算表达式 } , EL 表达式支持如下运算符:
empty 运算可以判断一个数据是否为空,如果为空,则输出 true,不为空输出 false。
以下几种情况为空:
1、值为 null 值的时候,为空
2、值为空串的时候,为空
3、值是 Object 类型数组,长度为零的时候
4、list 集合,元素个数为零
5、map 集合,元素个数为零
<%
// 1 、值为 null 值的时候,为空
request.setAttribute("emptyNull", null);
// 2 、值为空串的时候,为空
request.setAttribute("emptyStr", "");
// 3 、值是 Object 类型数组,长度为零的时候
request.setAttribute("emptyArr", new Object[]{});
// 4 、 list 集合,元素个数为零
List list = new ArrayList<>();
request.setAttribute("emptyList", list);
//list 集合,元素个数为零
List list1 = new ArrayList<>();
list1.add("abc");
request.setAttribute("emptyList1",list1);
// 5 、 map 集合,元素个数为零
Map map = new HashMap();
request.setAttribute("emptyMap", map);
map 集合,元素个数为1
Map map1 = new HashMap();
map1.put("key1", "value1");
request.setAttribute("emptyMap1", map1);
%>
${ empty emptyNull }
${ empty emptyStr }
${ empty emptyArr }
${ empty emptyList }
${ empty emptyList1 }
${ empty emptyMap }
${ empty emptyMap1 }
结果:
表达式 1?表达式 2:表达式 3 如果表达式 1 的值为真,返回表达式 2 的值,如果表达式 1 的值为假,返回表达式 3 的值。
示例: ${ 12 != 12 ? "表达式为真":" 表达式为假" }
.点运算,可以输出 Bean 对象中某个属性的值。 []中括号运算,可以输出有序集合中某个元素的值。 并且[]中括号运算,还可以输出 map 集合中 key 里含有特殊字符的 key 的值。
<%
Map map = new HashMap();
map.put("a.a.a", "aaaValue");
map.put("b+b+b", "bbbValue");
map.put("c-c-c", "cccValue");
map.put("d", "dValue");
map.put("e", "eValue");
map.put("f", "fValue");
request.setAttribute("map", map);
%>
${ map['a.a.a'] }
${ map["b+b+b"] }
${ map['c-c-c'] }
${ map.d }
${ map.e}
${ map.f}
结果:
EL 个达式中 11 个隐含对象,是 EL 表达式中自己定义的,可以直接使用。
例子:
<%
request.setAttribute("aaa","aaaValue");
session.setAttribute("bbb","bbbValue");
%>
${requestScope["aaa"] }
${sessionScope["bbb"]}
结果:
pageScope --->pageContext 域
requestScope ---> Request 域
sessionScope ---> Session 域
applicationScope --->ServletContext 域
<%
pageContext.setAttribute("key1", "pageContext1");
pageContext.setAttribute("key2", "pageContext2");
request.setAttribute("key2", "request");
session.setAttribute("key2", "session");
application.setAttribute("key2", "application");
%>
${ applicationScope.key2 }
${ sessionScope.key2 }
${ requestScope.key2 }
结果:
<%--
request.getScheme() 它可以获取请求的协议
request.getServerName() 获取请求的服务器 ip 或域名
request.getServerPort() 获取请求的服务器端口号
getContextPath() 获取当前工程路径
request.getMethod() 获取请求的方式( GET 或 POST )
request.getRemoteHost() 获取客户端的 ip 地址
session.getId() 获取会话的唯一标识
--%>
<%
pageContext.setAttribute("req", request);
%>
<%=request.getScheme() %>
1.协议: ${ req.scheme }
2.服务器 ip:${ pageContext.request.serverName }
3.服务器端口:${ pageContext.request.serverPort }
4.获取工程路径:${ pageContext.request.contextPath }
5.获取请求方法:${ pageContext.request.method }
6.获取客户端 ip 地址:${ pageContext.request.remoteHost }
7.获取会话的 id 编号:${ pageContext.session.id }
结果:
假设浏览器中请求地址为:
http://localhost:8080/JSPDemo/PersonOutput.jsp?username=愷龍&password=123456&teacher=耿祥义&teacher=柳淑琴
输出请求参数 username 的值:${ param.username }
输出请求参数 password 的值:${ param.password }
输出请求参数 username 的值:${ paramValues.username[0] }
输出请求参数 teacher 的值:${ paramValues.teacher[0] }
输出请求参数 teacher 的值:${ paramValues.teacher[1] }
结果:
输出请求头【User-Agent】的值:${ header['User-Agent'] }
输出请求头【Connection】的值:${ header.Connection }
输出请求头【User-Agent】的值:${ headerValues['User-Agent'][0] }
cookie Map
获取 Cookie 的名称:${ cookie.JSESSIONID.name }
获取 Cookie 的值:${ cookie.JSESSIONID.value }
initParam Map
web.xml 中的配置:
username
愷龍
age
21
jsp代码:
输出username 的值:${ initParam.username }
输出age 的值:${ initParam.age }
欢迎关注公众号:愚生浅末
留言与评论(共有 0 条评论) “” |