天天看點

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

目錄

1.EL 表達式簡介

2.EL 表達式搜尋域資料的順序

3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。

4.EL 表達式——運算

4.1)關系運算

4.2)邏輯運算

4.3)算數運算

4.3.1、empty運算

4.3.2、三元運算

4.3.3、“.”點運算 和 [] 中括号運算符

5.EL 表達式的11個隐含對象

5.1)EL 擷取四個特定域中的屬性

5.2)pageContext對象的使用

5.3)EL表達式其他隐含對象的使用

1.EL 表達式簡介

EL 表達式的全稱是:Expression Language。是表達式語言。

EL 表達式的什麼作用:EL 表達式主要是代替 jsp 頁面中的表達式腳本在 jsp 頁面中進行資料的輸出。

因為 EL 表達式在輸出資料的時候,要比 jsp 的表達式腳本要簡潔很多。

<%
        request.setAttribute("key", "值");
    %>
    表達式腳本輸出 key 的值是:
    <%=request.getAttribute("key1") == null ? "" : request.getAttribute("key1")%><br/>
    EL 表達式輸出 key 的值是:${key1}
           

EL 表達式的格式是:${表達式}

EL 表達式在輸出 null 值的時候,輸出的是空串。jsp 表達式腳本輸出 null 值的時候,輸出的是 null 字元串。

2.EL 表達式搜尋域資料的順序

EL 表達式主要是在 jsp 頁面中輸出資料。

主要是輸出域對象中的資料。

當四個域中都有相同的 key 的資料的時候,EL 表達式會按照四個域的從小到大的順序去進行搜尋,找到就輸出。

<%
        //往四個域中都儲存了相同的key的資料。
        request.setAttribute("key", "request");
        session.setAttribute("key", "session");
        application.setAttribute("key", "application");
        pageContext.setAttribute("key", "pageContext");
    %>
    ${ key }
           

3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。

練習:輸出 Person 類中普通屬性,數組屬性。list 集合屬性和 map 集合屬性。

Person類:

package pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author LIXICHEN
 * @create 2020-04-22 18:05
 */
public class Person {
    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 "Person{" +
                "name=" + name +
                ", phones=" + Arrays.toString(phones) +
                ", cities=" + cities +
                ", map=" + map +
                '}';
    }
}
           

輸出代碼:

<%@ page import="pojo.Person" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/4/22
  Time: 18:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>練習</title>
</head>
<body>

    <%
        Person person = new Person();
        person.setName("小明");
        person.setPhones(new String[]{"13611112222","18722223333","18699998888"});

        List<String> cities = new ArrayList<>();
        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");
        map.put("key4","value4");
        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中的cities集合中的個别元素值:${ person.cities[0] }<br>
    輸出Person中的map集合:${ person.map }<br>
    輸出Person中的map集合中某個key的值:${ person.map.key1 }<br>

</body>
</html>
           

結果截圖:

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

4.EL 表達式——運算

文法:${ 運算表達式 } , EL 表達式支援如下運算符:

4.1)關系運算

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

4.2)邏輯運算

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

4.3)算數運算

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

4.4)empty運算

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<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/>
           

4.5)三元運算

表達式 1?表達式 2:表達式 3

如果表達式 1 的值為真,傳回表達式 2 的值,如果表達式 1 的值為假,傳回表達式 3 的值。

代碼示例:

${ 12 != 12 ? "嘿嘿嘿":"嘻嘻嘻" }
           

4.6)“.”點運算 和 [] 中括号運算符

.點運算,可以輸出 Bean 對象中某個屬性的值。

[]中括号運算,可以輸出有序集合中某個元素的值。并且[]中括号運算,還可以輸出 map 集合中 key 裡含有特殊字元的 key 的值。

代碼示例:

<%
        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>
           

5.EL 表達式的11個隐含對象

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

5.1)EL 擷取四個特定域中的屬性

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

代碼示例:

<%
        pageContext.setAttribute("key1", "pageContext1");
        pageContext.setAttribute("key2", "pageContext2");
        request.setAttribute("key2", "request");
        session.setAttribute("key2", "session");
        application.setAttribute("key2", "application");
    %>
    ${ applicationScope.key2 }
           

5.2)pageContext對象的使用

代碼示例:

<%--
    request.getScheme() 它可以擷取請求的協定
    request.getServerName() 擷取請求的伺服器ip或域名
    request.getServerPort() 擷取請求的伺服器端口号
    getContextPath() 擷取目前工程路徑
    request.getMethod() 擷取請求的方式(GET或POST)
    request.getRemoteHost()  擷取用戶端的ip 位址
    session.getId() 擷取會話的唯一辨別
    --%>
    <%
        pageContext.setAttribute("req", request);
    %>
    <%=request.getScheme() %> <br>
    1.協定: ${ 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>
           

結果截圖:

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

5.3)EL表達式其他隐含對象的使用

EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象
EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象
EL 表達式1.EL 表達式簡介2.EL 表達式搜尋域資料的順序3.EL 表達式輸出Bean的普通屬性數組,List集合,map集合的屬性。4.EL 表達式——運算5.EL 表達式的11個隐含對象

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>
           

代碼示例:

輸出請求參數username的值:${ param.username } <br>
    輸出請求參數password的值:${ param.password } <br>

    輸出請求參數username的值:${ paramValues.username[0] } <br>
    輸出請求參數hobby的值:${ paramValues.hobby[0] } <br>
    輸出請求參數hobby的值:${ paramValues.hobby[1] } <br>
    <hr>
    輸出請求頭【User-Agent】的值:${ header['User-Agent'] } <br>
    輸出請求頭【Connection】的值:${ header.Connection } <br>
    輸出請求頭【User-Agent】的值:${ headerValues['User-Agent'][0] } <br>
    <hr>
    擷取Cookie的名稱:${ cookie.JSESSIONID.name } <br>
    擷取Cookie的值:${ cookie.JSESSIONID.value } <br>
    <hr>

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