1. What is an EL expression and what does it do?
- The full name of the EL expression is: Expression Language [expression language]
- What is the role of EL expression: EL expression is mainly to replace the expression script in the jsp page to output the data in the jsp page.
- Because EL expressions output data, they are much simpler than jsp expression scripts.
<body>
<%
request.setAttribute("key","值");
%>
表达式脚本输出 key 的值是:
<%=request.getAttribute("key1")==null?"":request.getAttribute("key1")%><br/>
EL 表达式输出 key 的值是:${key1}
</body>
Note:
The format of the${表达式}
EL expression is: When the EL expression outputs a null value, the output is an empty string.
When the jsp expression script outputs a null value, it outputs a null string.
Second, the order of EL expression search domain data
- EL expressions mainly output data in jsp pages. Mainly output data in domain objects.
- When all four fields have the same key data, the EL expression will search in the order of the four fields from small to large, and output it if found.
<body>
<%
//往四个域中都保存了相同的 key 的数据。
pageContext.setAttribute("key", "pageContext");
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
%>
${ key }
</body>
3. The EL expression outputs the common attributes of the Bean and the array attributes. List collection attribute, map collection attribute
i. Demand-output common attributes and array attributes in the Person class. List collection attribute and map collection attribute.
public class Person {
//
i.需求——输出 Person 类中普通属性,数组属性。list 集合属性和 map 集合属性。
private String name;
private String[] phones;
private List<String> cities;
private Map<String,Object> map;
//等等set,get,toString方法,不再一一赘述。
}
<body>
<%
Person person = new Person();
person.setName("张三啊");
person.setPhones(new String[]{"18610541354","18688886666","18699998888"});
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("p", person);
%>
输出 Person:${ p }<br/>
输出 Person 的 name 属性:${p.name} <br>
输出 Person 的 pnones 数组属性值:${p.phones[2]} <br>
输出 Person 的 cities 集合中的元素值:${p.cities} <br>
输出 Person 的 List 集合中个别元素值:${p.cities[2]} <br>
输出 Person 的 Map 集合: ${p.map} <br>
输出 Person 的 Map 集合中某个 key 的值: ${p.map.key3} <br>
输出 Person 的 age 属性:${p.age} <br>
</body>
Fourth, EL expression-operation
Syntax:, ${ 运算表达式 }
EL expression supports the following operators:
4.1 Relational operation
4.2 Logic operation
4.3 Arithmetic operations
i, empty operation
The empty operation can determine whether a piece of data is empty, if it is empty, it outputs true, but not empty, it outputs false.
The following situations are empty:
- When the value is null, it is empty
- When the value is an empty string, it is empty
- When the value is an array of type Object, the length is zero
- List collection, the number of elements is zero
- map collection with zero elements
<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<>();
//list.add("abc");
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>
iii. "." Dot operation and [] bracket operator
.
Point operation, you can output the value of a property in the Bean object.
[]
Square bracket operation can output the value of an element in an ordered set. And [] bracket operation, you can also output the value of the key with special characters in the key in the map collection.
<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>
Five, 11 hidden objects of EL expression
The 11 hidden objects in the EL expression are self-defined in the EL expression and can be used directly.
i. EL gets attributes in four specific domains
<body>
<%
pageContext.setAttribute("key1", "pageContext1");
pageContext.setAttribute("key2", "pageContext2");
request.setAttribute("key2", "request");
session.setAttribute("key2", "session");
application.setAttribute("key2", "application");
%>
${ applicationScope.key2 }
</body>
Reference: Shang Silicon Valley B Station Wang Zhenguo JavaWeb Video