EL 表达式

it2025-01-10  32

EL 表达式

EL 表达式概述EL 表达式搜索域数据的顺序EL 表达式输出 Bean 的普通属性、数组属性、List集合属性、map集合属性EL 表达式--运算关系运算逻辑运算算数运算empty 运算三元运算' . '点运算 和 [ ]中括号运算符 EL 表达式的11个隐含对象EL 获取四个特定域中的属性pageContext 对象的使用EL 表达式其他隐含对象的使用

EL 表达式概述

EL 表达式的全称是:Expression Language,是表达式语言。 EL 表达式的作用:EL 表达式主要是代替 jsp 页面中的表达式脚本在 jsp 页面中进行数据的输出。EL 表达式在输出数据的时候要比 jsp 的表达式脚本简洁很多。

EL 表达式的格式: ${表达式} EL 表达式在输出 null 值的时候,输出的是空串。jsp 表达式脚本输出 null 值的时候,输出的是 null 字符串。

EL 表达式搜索域数据的顺序

EL 表达式主要是在 jsp 页面中输出数据,输出的主要是域数据对象中的数据。

当四个域中都有相同的 key 的数据的时候,EL 表达式会按照四个域的从大到小的顺序去进行搜索,找到就输出。

EL 表达式输出 Bean 的普通属性、数组属性、List集合属性、map集合属性

Person 类:

public class Person { // 输出 Person 类中普通属性,数组属性,list集合属性和Map集合属性 private String name; private String[] phones; private List<String> cities; private Map<String,Object> map; public Person() { } public Person(String name, String[] phones, List<String> cities, Map<String, Object> map) { this.name = name; this.phones = phones; this.cities = cities; this.map = map; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getPhones() { return phones; } public void setPhones(String[] phones) { this.phones = phones; } public List<String> getCities() { return cities; } public void setCities(List<String> cities) { this.cities = cities; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } @Override public String toString() { return "pojo{" + "name=" + name + ", phones=" + Arrays.toString(phones) + ", cities=" + cities + ", map=" + map + '}'; } }

jsp 文件代码:

<body> <% Person person = new Person(); person.setName("韩金轮"); person.setPhones(new String[]{"1233333321","1322222322","145555555454"}); List<String> cities = new ArrayList<String>(); cities.add("芜湖"); cities.add("北京"); cities.add("天津"); person.setCities(cities); Map<String,Object> map = new HashMap<>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); person.setMap(map); pageContext.setAttribute("person",person); %> 输出Person:${ person } <br> 输出Person的name属性:${person.name} <br> 输出Person的phones数组属性值:${person.phones[0]}<br> 输出Person的cities集合中的元素值:${person.cities} <br> 输出Person的List集合中个别元素值:${person.cities[2]} <br> 输出Person的Map集合:${person.map} <br> 输出Person的Map集合中的某个key的值: ${person.map.key1} <br> </body>

EL 表达式–运算

关系运算

逻辑运算

算数运算

empty 运算

empty 运算可以判断一个数据是否为空,如果为空,则输出true,不为空输出 false。

以下几种情况为空:

值为 null 值的时候,为空值为空串的时候,为空值是 Object 类型数组,长度为零的时候list 集合,元素个数为零map 集合,元素个数为零 <body> <% // 1. 值为 null 值的时候,为空 request.setAttribute("emptyNull",null); // 2. 值为空串的时候,为空 request.setAttribute("emptyStr",""); // 3. 值是 Object 类型数组,长度为零的时候 request.setAttribute("emptyArr",new Object[]{}); // 4. list 集合,元素个数为零 List<String> list = new ArrayList<>(); request.setAttribute("emptyList", list); // 5. map 集合,元素个数为零 Map<String,Object> map = new HashMap<String,Object>(); request.setAttribute("emptyMap",map); %> ${ empty emptyNull } <br/> ${ empty emptyStr } <br/> ${ empty emptyArr } <br/> ${ empty emptyList } <br/> ${ empty emptyMap } <br/> </body>

三元运算

表达式1 ? 表达式 2 : 表达式 3 如果表达式 1 的值为真,返回表达式 2 的值,如果表达式 1 的值为假,返回表达式 3 的值。

’ . '点运算 和 [ ]中括号运算符

. 点运算,可以输出 Bean 对象中某个属性的值。 [] 中括号运算,可以输出有序集合中某个元素的值。 [] 中括号运算,还可以输出 map 集合中 key 里含有特殊字符的 key 的值。

代码示例:

<body> <% Map<String,Object> map = new HashMap<String,Object>(); map.put("a.a.a","aaaValue"); map.put("b+b+b","bbbValue"); map.put("c-c-c","cccValue"); request.setAttribute("map",map); %> ${ map['a.a.a'] } <br> ${ map['b+b+b'] } <br> ${ map['c-c-c'] } <br> </body>

EL 表达式的11个隐含对象

EL 表达式中有 11 个隐含对象,由 EL 表达式定义,可以直接使用。

EL 获取四个特定域中的属性

代码示例:

<body> <% pageContext.setAttribute("key1","pageContext1"); pageContext.setAttribute("key2","pageContext2"); request.setAttribute("key2","request"); session.setAttribute("key2","session"); application.setAttribute("key","application"); %> ${ pageScope.key1 } ${ pageScope.key2 } ${ requestScope.key2 } ${ sessionScope.key2 } ${ applicationScope.key } </body>

pageContext 对象的使用

pageContext.jsp 文件代码示例:

<body> <%-- request.getScheme() 它可以获取请求的协议 request.getServerName() 获取请求的服务器ip或域名 request.getServerPort() 获取请求的服务器端口号 getContextPath() 获取当前工程路径 request.getMethod() 获取请求的方式(GET 或 POST) request.getRemoteHost() 获取客户端的ip地址 session.getId() 获取会话的唯一标识 --%> 1.协议: ${ pageContext.request.scheme } <br> 2.服务器ip: ${ pageContext.request.serverName } <br> 3.服务器端口: ${ pageContext.request.serverPort } <br> 4.获取工程路径: ${ pageContext.request.contextPath } <br> 5.获取请求方法: ${ pageContext.request.method } <br> 6.获取客户端ip地址: ${ pageContext.request.remoteHost } <br> 7.获取会话的ip编号: ${ pageContext.session.id } <br> <%--可以将request对象封装到pageContext对象中--%> <% pageContext.setAttribute("req",request); %> 1.协议: ${ req.scheme } <br> 2.服务器ip: ${ req.serverName } <br> 3.服务器端口: ${ req.serverPort } <br> 4.获取工程路径: ${ req.contextPath } <br> 5.获取请求方法: ${ req.method } <br> 6.获取客户端ip地址: ${ req.remoteHost } <br> 7.获取会话的ip编号: ${ pageContext.session.id } <br> </body>

EL 表达式其他隐含对象的使用

other_el_obj.jsp文件示例代码:

<%-- Created by IntelliJ IDEA. User: Administrator Date: 2020/10/22 Time: 10:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 输出请求参数 username 的值:${ param.username } <br> 输出请求参数 password 的值:${ param.password } <br> 输出请求参数 username 的值:${ paramValues.username[0] } <br> 输出请求参数 hobby 的值:${ paramValues.hobby[0] } <br> 输出请求参数 hobby 的值:${ paramValues.hobby[1] } <br> </body> </html>

请求地址:

http://localhost:8080/09_EL_JSTL/other_el_obj.jsp?username=wzg168&password=666666&hobby=java&hobby=cpp
最新回复(0)