1. 程式人生 > 其它 >EL表示式詳解_javaweb_jsp

EL表示式詳解_javaweb_jsp

技術標籤:java

1.EL表示式介紹

Expression Language表示式語言
是一種在JSP頁面獲取資料的簡單方式(只能獲取資料,不能設定資料)
在JSP2.0開始引入概念

語法格式
在JSP頁面的任何靜態部分均可通過:

${expression}
來獲取到指定表示式的值

2.EL獲取資料(從四大域中獲取屬性)

EL只能從四大域中獲取屬性

2.1如果沒有使用EL的內建物件,則查詢資料順序是依次按照由小到大範圍從四大域中查詢指定名稱的屬性值

- pageContext<request<session<application

        <%@ page language="java" contentType="text/html; charset=UTF-8"
            pageEncoding="UTF-8"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
        </head>
        <body>
            <%
                pageContext.setAttribute("name", "linjie");
                request.setAttribute("name", "lucy");
                session.setAttribute("name", "king");
                application.setAttribute("name", "bilibili");
            %>
            name=${name }
        </body>
        </html>

在這裡插入圖片描述

2.2 使用EL內建物件,從指定域中獲取資料,提高了查詢效率

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        pageContext.setAttribute("name", "linjie");
        request.setAttribute("name", "lucy");
        session.setAttribute("name", "king");
        application.setAttribute("name", "bilibili");
    %>
    name=${applicationScope.name }
</body>
</html>

在這裡插入圖片描述

2.3 EL中的內建物件

EL有11個內建物件,這裡主要講域屬性相關的4個和其他4個
EL的11個內建物件,除了pageContext以外,其他10個內建物件的型別都是java.util.Map型別

pageScope:從page範圍域屬性空間中查詢指定的key
requestScope:從request範圍域屬性空間中查詢指定的key
sessionScope:從session範圍域屬性空間中查詢指定的key
applicationScope:從application範圍域屬性空間中查詢指定的key

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        pageContext.setAttribute("name", "linjie");
        request.setAttribute("name", "lucy");
        session.setAttribute("name", "king");
        application.setAttribute("name", "bilibili");
    %>

    name=${applicationScope.name }<br>
    name=${pageScope.name }<br>
    name=${sessionScope.name }<br>
    name=${requestScope.name }<br>
</body>
</html>

在這裡插入圖片描述

3.EL訪問Bean的屬性

EL訪問Bean屬性
EL可以通過${key.屬性}的方式獲取到指定值,其底層實際呼叫的是該物件的相應屬性的get方法

Demo.java

package linjie.com;
/*
 *Bean 
 */
public class Demo {
    private String name;
    private int age;
    public Demo(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return super.toString();
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    import="linjie.com.Demo"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Demo test=new Demo("linjie",12);
        request.setAttribute("ELttt", test);
    %>
    name=${requestScope.ELttt.name }<br>
    age=${requestScope.ELttt.age }<br>

    <!-- 若訪問為null的物件的屬性,EL是不會丟擲空指標異常的,只是不顯示而已 -->
    names=${requestScope.ELtttxx.name }<br>

</body>
</html>

在這裡插入圖片描述

4.EL訪問陣列中的資料

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>     
<body>
    <%
        String[] names={"xlj","lucy","king"};
        pageContext.setAttribute("names", names);
    %>
    name[1]=${names[1] }<br>

    <!-- 若訪問的陣列元素下標超出了陣列下標上限,EL不會丟擲越界異常,只是不顯示 -->
    names[5]=${names[5] }<br>
</body>
</html>

Stu.java

package linjie.com;
/*
 *Bean 
 */
public class Stu {
    private String sname;
    private String address;
    public Stu() {
        super();
    }

    public Stu(String sname, String address) {
        super();
        this.sname = sname;
        this.address = address;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return super.toString();
    }


}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    import="linjie.com.*"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <%
        Stu[] stus=new Stu[3];
        stus[0]=new Stu("xlj","A");
        stus[1]=new Stu("lucy","B");
        stus[2]=new Stu("kingA","C");
        pageContext.setAttribute("stus",stus);
    %>
    stus[1].Sname=${stus[1].sname }
</body>
</html>

在這裡插入圖片描述

5.EL獲取list中資料

<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        List<String> names=new ArrayList<String>();
        names.add("xlj");
        names.add("lucy");
        pageContext.setAttribute("names", names);
    %>

    <!-- 因為List底層是陣列,所以可以這樣寫 -->
    names[1]=${names[1] }<br>
</body>
</html>

在這裡插入圖片描述
EL可以通過索引訪問List,但無法訪問Set。因為Set中沒有索引概念

6.EL訪問Map

<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("age", 20);
        map.put("name", "xlj");
        pageContext.setAttribute("map", map);
    %>
    name=${map.name }<br>
    age=${map.age }<br>
</body>
</html>

在這裡插入圖片描述

7.EL中的運算子(empty)

算術運算子:+、-、*、/、%(不支援++、–)
關係運算符:==、!=、>、>=、<、<=
邏輯運算子:!、&&、||、not、and、or
條件運算子:?:
取值運算子:[]、點號

<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String name1=null;
        String name2="";
        List<String> name3=new ArrayList<String>();

        pageContext.setAttribute("name1", name1);
        pageContext.setAttribute("name2", name2);
        pageContext.setAttribute("name3", name3);
    %>
    empty對於沒有定義的變數,運算結果為true:
    empty namex=${empty namex }<br>

    empty對於null的引用,運算結果為true:
    empty name1=${empty name1 }<br>

    empty對於為空串的String引用,運算結果為true:
    empty name2=${empty name2 }<br>

    empty對於沒有元素的陣列或集合,運算結果為true:
    empty name3=${empty name3 }<br>
</body>
</html>

在這裡插入圖片描述