EL expression study notes (super detailed)

1. What is EL expression and what does EL expression do?

The full name of EL expression is: Expression Language is an expression language.

What is the function of EL expression: EL expression mainly replaces the expression script in the jsp page to output data in the jsp page . Because EL expressions are much more concise than jsp expression scripts when outputting data.

a.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--先往页面中保存一些数据--%>
    <%
        request.setAttribute("key","值");
    %>
    表达式脚本输出key的值是:<%=request.getAttribute("key")%><br>
    EL表达式输出key的值是:${key}<br>
    <br>
    输出不存在的值:<br>
    表达式脚本输出key的值是:<%=request.getAttribute("key1")%><br>
    EL表达式输出key的值是:${key1}<br>
</body>
</html>

Show results:
Insert picture description here

Note :

The format of EL expression is: ${Expression}

When the EL expression outputs a null value, the output is an empty string.

When the jsp expression script outputs a null value, the output is a null string.

2. EL expression search order of domain data

EL expression is mainly to output data in jsp page.

Mainly output the data in the domain object.

b.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        //往四个域中都保存了相同的key的数据
        pageContext.setAttribute("key","pageContext");
        request.setAttribute("key","request");
        session.setAttribute("key","session");
        application.setAttribute("key","application");
    %>
    ${key}
</body>
</html>

Show results:
Insert picture description here

After the pageContext is annotated:

Insert picture description here

Comment out the request again:

Insert picture description here

Annotate the session again, close the browser and open b.jsp:

Insert picture description here

Note: When the 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 when found.

Sort from small to large: pageContext ====>>> request ====>>> session ====>>> application

3. The EL expression outputs the common attributes and array attributes of the Bean. List collection properties, map collection properties

3.1 Requirement: output common attributes and array attributes in the Person class. List collection attributes and map collection attributes.

src/com/pojo/Person.java:

public class Person {
    
    
//    需求i. 需求——输出 Person 类中普通属性,数组属性。list 集合属性和 map 集合属性。
    private String name;
    private String[] phones;
    private List<String> cities;
    private Map<String,Object> map;

c.jsp:

<body>
    <%
        Person person = new Person();
        person.setName("旭哥好帅");
        person.setPhones(new String[]{"10086","10010","12306"});

        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);
        //把 person 放到四个域当中,任何一个都可以
        pageContext.setAttribute("p",person);
    %>

    输出 person: ${p} <br><br>
    输出 person.name 属性: ${p.name} <br><br>
    输出 person的phones 数组属性: ${p.phones}  这里输出的是数组的地址<br>
    输出 person的phones[0] 数组属性: ${p.phones[0]} <br><br>
    输出 person的List集合中的所有元素值: ${p.cities}  这里输出整个 list 集合中的元素<br>
    输出 person的List集合中的个别元素值: ${p.cities[0]} <br><br>
    输出 person的Map集合中的所有元素值: ${p.map} <br>
    输出 person的Map集合中的某个key的值: ${p.map.key1}  这里输出 key1 <br><br><br><br>

</body>

Show results:

Insert picture description here

If you write another getAge() method in person ( note: there is no age attribute in the Person class )

public int getAge() {
    
    
    return 20;
}

Show results:

Insert picture description here

The age attribute can still be displayed.

This is because: in EL expressions, what you are looking for is not an attribute, but a get method

4. EL expression-operation

Syntax: ${ operation expression}

EL expressions support the following operators:

4.1 Relational operations

Relational operator Description example result
== or eq equal ${ 5 == 5} or ${ 5 eq 5} true
!= or ne not equal to ${ 5 != 5} or ${ 5 ne 5} false
< 或 lt Less than ${ 3 < 5 } 或 ${ 3 lt 5 } true
> Or gt more than the ${ 2> 10} or ${ 2 gt 10} false
<= or le Less than or equal to ${ 5 <= 12} or ${ 5 le 12} true
>= or ge greater or equal to ${ 3 >= 5} or ${ 3 ge 5} false

4.2 Logical operations

Logical Operators Description example result
&& 或 and AND operation ${ 12 == 12 && 12 < 11 } 或 ${ 12 == 12 and 12 < 11 } false
|| 或 or OR operation ${ 12 == 12 || 12 < 11 } 或 ${ 12 == 12 or 12 < 11 } true
! Or not Negation operation ${ !true } 或 ${not true } false

4.3 Arithmetic operations

Arithmetic Operator Description example result
+ addition ${ 12 + 18 } 30
- Subtraction ${ 18 - 8 } 10
* multiplication ${ 12 * 12 } 144
/ Or div division ${ 144/12} or ${ 144 div 12} 12
% Or mod Modulo ${ 144% 10} or ${ 144 mod 10} 4

4.4 empty operation

The empty operation can judge whether a piece of data is empty, if it is empty, it will output true, and if it is not empty, it will output false.

It is empty in the following situations:

  1. When the value is null, it is empty
  2. When the value is an empty string, it is empty
  3. The value is an Object type array, when the length is zero
  4. list collection, the number of elements is zero
  5. map collection, the number of elements is zero
<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>();
    // map.put("key1", "value1");
        request.setAttribute("emptyMap", map);
    %>
    ${ empty emptyNull } <br/>
    ${ empty emptyStr } <br/>
    ${ empty emptyArr } <br/>
    ${ empty emptyList } <br/>
    ${ empty emptyMap } <br/>
</body>

4.5 Ternary Operation

Expression 1? Expression 2: Expression 3

If the value of expression 1 is true, return the value of expression 2, if the value of expression 1 is false, return the value of expression 3

Example:

${ 12 != 12   ?   "旭哥好帅!" : "旭哥太帅辣!" }

4.6 "." dot operation and [] bracket operator

. Point operation, you can output the value of an attribute in the Bean object.

[] Bracket operation can output the value of an element in an ordered set.

And [] bracket operation can also output the value of the key containing special characters in the key of the map set.

<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);
    %>
    <%--输出某个key--%>
    ${ map['a.a.a'] } <br>      <%--不是 ${ map.a.a.a } --%>
    ${ map["b+b+b"] } <br>
    ${ map['c-c-c'] } <br>
</body>

5. The 11 hidden objects of EL expressions

The 11 implicit objects in the EL expression are defined by themselves in the EL expression and can be used directly.

variable Types of effect
pageContext PageContextImpl It can get nine built-in objects in jsp
pageScope Map<String,Object> It can get the data in the pageContext domain
requestScope Map<String,Object> It can get the data in the Request field
sessionScope Map<String,Object> It can get the data in the Session field
applicationScope Map<String,Object> It can get the data in the ServletContext domain
param Map<String,String> It can get the value of the request parameter
paramValues Map<String,String[]> It can also get the value of the request parameter and use it when getting multiple values
header Map<String,String> It can get the information of the request header
headerValues Map<String,String[]> It can get the information of the request header, it can get multiple values
cookie Map<String,Cookie> It can get the cookie information of the current request
initParam Map<String,String> It can get the context parameters configured in web.xml

5.1 EL obtains attributes in four specific domains

pageScope ====== pageContext 域

requestScope ====== Request 域

sessionScope ====== Session 域

applicationScope ====== ServletContext 域

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

5.2 Use of pageContext object

  1. protocol:
  2. Server ip:
  3. Server port:
  4. Get the project path:
  5. Get request method:
  6. Get the client ip address:
  7. Get the id number of the session:
<body>
    <%--把request放到pageContext中,用req表示,这样更加简洁--%>
    <%
        pageContext.setAttribute("req", request);
    %>

    <%--表达式脚本输出
    request.getScheme() 它可以获取请求的协议
    request.getServerName() 获取请求的服务器 ip 或域名
    request.getServerPort() 获取请求的服务器端口号
    getContextPath() 获取当前工程路径
    request.getMethod() 获取请求的方式(GET 或 POST)
    request.getRemoteHost() 获取客户端的 ip 地址
    session.getId() 获取会话的唯一标识
    --%>
    <%=request.getScheme() %> <br>

    1.协议(pageContext.request.scheme): ${ pageContext.request.scheme }<br>
    1.协议(简化写法:req.scheme): ${ req.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.获取会话的 id 编号:${ pageContext.session.id }<br>
</body>

5.3 Use of other implicit objects in EL expressions

1) param, paramValues

param Map<String,String> It can get the value of the request parameter

paramValues ​​Map<String,String[]> It can also get the value of the request parameter, which is used when getting multiple values

Output param directly:

<body>
    ${ param }
</body>

Visit http://localhost:8080/09_EL_JSTL/other_el_obj.jsp, the effect display:

Insert picture description here

Here param represents the requested parameter, which is a Map type. There are no parameters at this time, so there is only one {}

Visit http://localhost:8080/09_EL_JSTL/other_el_obj.jsp?username=abc123, effect display:

Insert picture description here

访问http://localhost:8080/09_EL_JSTL/other_el_obj.jsp?username=abc123&password=123456:

Insert picture description here

other_el_obj.jsp:

<body>
    ${ param } <br>
    输出请求参数 username 的值:${ param.username } <br>
    输出请求参数 password 的值:${ param.password } <br>
    输出请求参数 username 的值:${ paramValues.username[0] } <br>
    输出请求参数 hobby 的值:${ paramValues.hobby[0] } <br>
    输出请求参数 hobby 的值:${ paramValues.hobby[1] } <br>
</body>

Show results:

Insert picture description here

2)header, headerValues

header Map<String,String> It can get the information of the request header

headerValues ​​Map<String,String[]> It can get the information of the request header, it can get multiple values

输出请求头【User-Agent】的值:${ header['User-Agent'] } <br>
输出请求头【Connection】的值:${ header.Connection } <br>
输出请求头【User-Agent】的值:${ headerValues['User-Agent'][0] } <br>

Insert picture description here

3)cookie

cookie Map<String,Cookie> It can get the cookie information of the current request

获取 Cookie 的名称:${ cookie.JSESSIONID.name } <br>
获取 Cookie 的值:${ cookie.JSESSIONID.value } <br>

Insert picture description here

4) initParam

initParam Map<String,String> It can get the context parameters configured in web.xml

输出&lt;Context-param&gt;username 的值:${ initParam.username } <br>
输出&lt;Context-param&gt;url 的值:${ initParam.url } <br>

web.xml:

<context-param>
    <param-name>username</param-name>
    <param-value>root</param-value>
</context-param>

<context-param>
    <param-name>url</param-name>
    <param-value>jdbc:mysql:///test</param-value>
</context-param>

Sample output:

p<String,String>
It can get the context parameters configured in web.xml

输出&lt;Context-param&gt;username 的值:${ initParam.username } <br>
输出&lt;Context-param&gt;url 的值:${ initParam.url } <br>

web.xml:

<context-param>
    <param-name>username</param-name>
    <param-value>root</param-value>
</context-param>

<context-param>
    <param-name>url</param-name>
    <param-value>jdbc:mysql:///test</param-value>
</context-param>

Sample output:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45024585/article/details/112747243