EL表達式相關概念以及通過EL表達式獲取域中的值

一、EL表達式相關概念

  1. 概念:Expression Language 表達式語言。
  2. 作用:替換和簡化jsp頁面中Java代碼的編寫。
  3. 語法:${表達式}
  4. 注意:
    ①、jsp默認支持el表達式的。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

在這種情況下,el表達式是被支持的。例如

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${3>4}
</body>
</html>

在瀏覽器上顯示的直接是false(瀏覽器直接判斷3不大於4).
但是如果添加isELIgnored="true"這個條件,那麼瀏覽器就會忽略el的表達式。isELIgnored默認值是false。

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="true" %>

上面的方法只是將頁面全部的el語法都忽略,也可以在$ 前面添加一個 / ,這樣就可以忽略當前這個el表達式,這種方法更加靈活的顯示el語句字符串

/$ {3>4}
  1. 使用:
    (1)運算符:
    ①. 算數運算符: +, -, *, /(div), %(mod)
    ②. 比較運算符: > ,<, >=, <=, ==, !=
    ③. 邏輯運算符: &&(and),||(or) ,!(not)
    ④. 空運算符: empty
    · 功能:用於判斷字符串、集合、數組對象是否爲null或者長度是否爲0
    · ${empty list}:判斷字符串、集合、數組對象是否爲null或者長度爲0
    · ${not empty str}:表示判斷字符串、集合、數組對象是否不爲null 並且 長度>0
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${3>4}
    /$ {3>4}
    <h4>empty運算符</h4>
    <%
        String str = "abc";
        request.setAttribute("str",str);

        List list = new ArrayList();
        request.setAttribute("list",list);
    %>

    ${empty str}<br><%--判斷str是否爲null,獲取判斷長度是否爲0--%>
    ${not empty list}<%--表示判斷字符串、集合、數組對象是否不爲null 並且 長度>0--%>
</body>
</html>

在這裏插入圖片描述

二、EL獲取域中存儲的值

  1. el表達式只能從域對象中獲取值

  2. 語法:
    (1)$ {域名稱.鍵名}:從指定域中獲取指定鍵的值
    ·域名稱
    ①. pageScope從pageContext域中獲取值。
    ②. requestScope從request域中獲取值。
    ③. sessionScope從session域中獲取值。
    ④. applicationScope從application(ServletContext)域中獲取值。
    ·舉例:在request域中存儲了name=張三
    ·獲取:${requestScope.name}

    (2)${鍵名}:表示依次從最小的域中查找是否有該鍵對應的值,直到找到爲止。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el獲取域中的數據</title>
</head>
<body>
    <%
        //在域中存儲數據
        session.setAttribute("name","李四");
        
        request.setAttribute("name","張三");
        session.setAttribute("age","23");
    %>
<h3>el獲取值</h3>
${requestScope.name}
${sessionScope.age}

${name}
</body>
</html>

在這裏插入圖片描述

2.1 獲取對象、List集合、Map集合的值

  1. 對象:${域名稱.鍵名.屬性名}
  • 本質上會去調用對象的getter方法
    創建User.java文件:
package cn.itcast.domain;

import java.text.SimpleDateFormat;
import java.util.Date;

public class User {
    private String name;
    private String age;
    private Date birthday;

    /**
     * 邏輯視圖
     * @return
     */
    public String getBirStr(){
        if (birthday != null){
            //1.格式化日期對象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //2.返回字符串即可
            return  sdf.format(birthday);
        }else {
            return "";
        }
    }
    public String getName() {
        return name;
    }
   public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
<%@ page import="cn.itcast.domain.User" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el獲取數據</title>
</head>
<body>
    <%
        User user = new User();
        user.setName("張三");
        user.setAge("23");
        user.setBirthday(new Date());

        request.setAttribute("u",user);
    %>
    <h3>el獲取對象中的值</h3>
    ${requestScope.u}<br>

<%--
    通過的是對象的屬性來獲取
    setter或getter方法,去掉set或get,在將剩餘部分,首字母變爲小寫
    setName--->Name--->name
--%>
    ${requestScope.u.name}<br>
    ${requestScope.u.age}<br>
    ${requestScope.u.birthday}<br>
    ${requestScope.u.birthday.year}<br>

    ${u.birStr}<br>
</body>
</html>

在這裏插入圖片描述

2.2 獲取List集合的值

  1. List集合:${域名稱.鍵名[索引]}
<%@ page import="cn.itcast.domain.User" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el獲取數據</title>
</head>
<body>
    <%
        User user = new User();
        user.setName("張三");
        user.setAge("23");
        user.setBirthday(new Date());

        request.setAttribute("u",user);

        List list = new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add(user);//在list集合裏面存儲user對象

        request.setAttribute("list",list);
    %>


    <h3>el獲取List中的值</h3>
    ${list}<br>
    ${list[0]}<br> <%--獲取第一個元素--%>
    ${list[1]}<br> <%--獲取第二個元素--%>
    ${list[10]}<br> <%--獲取第十個元素的值,角標越界,並不會報錯。也不會在顯示什麼--%>
    ${list[2].name}
</body>
</html>

在這裏插入圖片描述

2.3 獲取Map集合的值

Map集合:
(1)$ {域名稱.鍵名.key名稱}
(2)$ {域名稱.鍵名[“key名稱”]}

<%@ page import="cn.itcast.domain.User" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el獲取數據</title>
</head>
<body>
    <%
        User user = new User();
        user.setName("張三");
        user.setAge("23");
        user.setBirthday(new Date());

        request.setAttribute("u",user);

        Map map = new HashMap();
        map.put("sname","李四");
        map.put("gender","男");
        map.put("user",user);//在Map集合裏面存儲user對象

        request.setAttribute("map",map);
    %>

    <h3>el獲取Map中的值</h3>
    ${map.gender}<br>
    ${map["gender"]}<br>
    ${map.user.name}<br>

</body>
</html>

在這裏插入圖片描述

三、EL表達式隱式對象:

  • el表達式中有11個隱式對象
  • pageContext:獲取jsp其他八個內置對象
  • ${pageContext.request.contextPath}:動態獲取虛擬目錄
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el隱式對象</title>
</head>
<body>
    ${pageContext.request}<br>
    <h4>在jsp頁面動態獲取虛擬目錄</h4>
    ${pageContext.request.contextPath}<br><%--獲取虛擬目錄--%>
</body>
</html>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章