EL表达式

概念

EL使JSP写起来更简单、简洁。主要用于获取作用域中的数据。

作用

用于替换作用域对象.getAttribute("name");

EL的应用

  • ${scope.name} 获取集体某个作用域中的数据

  • ${name} 获取作用域中的数据,逐级查找(pageContext、request、session、application)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el表达式</title>
</head>
<body>
    <%
        request.setAttribute("key1","value1");
        session.setAttribute("key2","value2");
        application.setAttribute("key3","value3");
    %>

    <%--通过域对象获取值--%>
    <%=request.getAttribute("key1")%><br>
    <%=session.getAttribute("key2")%><br>
    <%=application.getAttribute("key3")%><br>
    <hr/>

    <%--el表达式获取值--%>
    ${requestScope.key1}<br>
    ${sessionScope.key2}<br>
    ${applicationScope.key3}<br>
    <hr/>

    <%--el表达式简写,不推荐
        会从requestScope、sessionScope、applicationScope 搜索值,找不到返回空字符串""
    --%>
    ${key1}<br>
    ${key2}<br>
    ${key3}<br>
    ${key4}<br>

</body>
</html>

EL获取引用类型

使用EL获取作用域中的对象调用属性时,只能访问对象的get方法,必须遵守命名规范定义

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        User user = new User("张三",18);
        request.setAttribute("user",user);
    %>

    ${requestScope.user.name}<br>
    ${requestScope.user.age}<br><!--调用get方法-->
</body>
</html>

EL获取数组、集合的元素

EL可以获取Array、List、Map中的元素,Set用于没下标,无法直接访问,后续可遍历。

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>array</title>
</head>
<body>
    <%
        int[] a = {1,2,3,4};

        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);

        Map<String,String> map = new HashMap<>();
        map.put("河南","豫");
        map.put("北京","京");
        map.put("上海","沪");

        //向域中存入数组,集合
        request.setAttribute("a",a);
        request.setAttribute("arrayList",arrayList);
        request.setAttribute("map",map);
    %>

    ${a[0]}<br>
    ${a[1]}<br>
    ${a[2]}<br>
    ${a[4]}<br><hr/>

    ${arrayList[0]}<br>
    ${arrayList.get(1)}<br>
    ${arrayList[2]}<br><hr/>

    ${map.get("河南")}<br>
    ${map.北京}<br>
    ${map["上海"]}<br>

</body>
</html>

EL的运算符

操作符 描述
. 访问一个Bean属性或者一个映射条目
[] 访问一个数组或链表的元素
+
-
*
/ or div
% or mod 取模
== or eq 等于
!= or ne 不等于
< or lt 小于
> or gt 大于
<= or le 小于等于
>= or ge 大于等于
&& or and
|| or or
! or not 取反
empty 是否为空(""或null)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>运算符</title>
</head>
<body>
    ${ 2 +3 }
    ${ 2 -3 }
    ${ 2 *3 }
    ${ 2 / 3 }
    ${ 2 % 3 }
    <hr/>
    ${ 2 > 3 }
    ${ 2 >= 3 }
    ${ 2 < 3 }
    ${ 2 <= 3 }
    ${ 2 != 3 }
    <hr/>
    ${true && true}
    ${true || true}
    <hr/>
    ${empty null}

</body>
</html>

隐式对象

隐式对象 描述
pageScope page作用域
requestScope request作用域
sessionScope session作用域
applicationScope application作用域
param Request对象的参数,字符串
paramValues Request对象的参数,字符串集合
header HTTP信息头,字符串
headerValues HTTP信息头,字符串集合
initParam 上下文初始化参数
cookie Cookie值
pageContext 当前页面的pageContext
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--获取项目路径--%>
    ${pageContext.request.contextPath}<br>

    <%--获取cookie--%>
    <input type="text" name="username" value="${cookie.username.value}">
    <input type="password" name="password" value="${cookie.password.value}">
</body>
</html>

https://www.cnblogs.com/caoyc/p/5569688.html

--------------- 我每一次回头,都感觉自己不够努力,所以我不再回头。 ---------------
原文地址:https://www.cnblogs.com/zjw-blog/p/13929421.html