本文最后更新于:December 3, 2021 pm
                
              
            
            
              EL语言的灵感来自于ECMAScript和XPath表达式语言。EL是JSP 2.0增加的技术规范,其全称是表达式语言(Expression Language)。EL 是为了使JSP写起来更加简单。主要用于获取作用域中的数据。EL表达式语言是一种简单的语言,提供了在JSP中简化表达式的方法,目的是为了尽量减少JSP页面中的Java代码,使得JSP页面的处理程序编写起来更加简洁,便于开发和维护。
目录
1.EL获取基本类型、字符串
- ${scope.name} 获取具体某个作用域中的数据。scope表示作用域。
 
- ${name} 获取作用域中的数据,逐级进行查找(从小到大),pageContext(当前)、request(一次请求)、session(一次会话)、application(就是ServletContext,整个项目)。先找到谁用谁。
 
对照示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
   | <%--   Created by IntelliJ IDEA.   User: DragonOne   Date: 2021/8/23   Time: 21:48   To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head>     <title>EL表达式</title> </head> <body>     <%         request.setAttribute("req","request");         session.setAttribute("ses","session");         application.setAttribute("app","application");
          request.setAttribute("name","loong");         session.setAttribute("name","loong");         application.setAttribute("name","loong");     %>
  <%--    通过作用域对象获取数据--%>     <h1><%=request.getAttribute("req")%></h1>     <h1><%=session.getAttribute("ses")%></h1>     <h1><%=application.getAttribute("app")%></h1>
      <hr> <%--    通过EL表达式获取数据--%>     <h1>${requestScope.req}</h1>     <h1>${sessionScope.ses}</h1>     <h1>${applicationScope.app}</h1>
      <hr> <%--    EL方法二--%>
      <h1>${name}</h1>     <h1>${name}</h1>     <h1>${name}</h1>
  </body> </html>
 
 
  | 
 
在网页上可见,前两种的输出都是一样的。而最后一种输出的都是一样的,说明都是同一个,因为都是从最小的request作用域中拿取的。
1.1 EL和JSP脚本的区别
<%=request.getAttribute() %> 如果没有找到返回 null;${requestScope.name} 没有找到返回空(””)。
2.EL获取引用类型
使用EL获取作用域中的对象调用属性时,只能访问对象的get()方法,get()方法的定义必须遵守命名规范定义。
${scope.a.name} 是通过调用getName()方法实现的。
示例:
 | <body>     <%         Person root = new Person("loong","tothefor");         request.setAttribute("roots",root);     %> 		${root} <!--获取的是地址-->     ${requestScope.roots.username}     ${requestScope.roots.password}
  </body>
 
  | 
 
**需要注意的是:${requestScope.root.username}和${requestScope.root.password}中的username和password不是通过直接属性名拿到的,而是通过get()方法拿到的,如果Person类中没有get()方法,则无法获取。 **
3.EL获取数组、集合
EL还可以获取Array、List、Map中的元素,Set由于没有下标,无法直接访问元素,但可遍历访问。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   | <%@ page import="com.tothefor.entity.Person" %> <%@ page import="java.lang.String" %> <%@ page import="java.util.*" %> <%--   Created by IntelliJ IDEA.   User: dragonone   Date: 2021/8/23   Time: 21:48   To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head>     <title>EL表达式</title> </head> <body>     <%         int[] array = new int[]{1,2,3,4,5};         request.setAttribute("arr",array);
          List<String> list = new ArrayList<>();         list.add("A");         list.add("B");         list.add("C");         request.setAttribute("li",list);
          Map<String,String> map = new HashMap<>();         map.put("CN","中国");         map.put("US","M国");         map.put("IT","意大利");         request.setAttribute("ma",map);
      %>
      ${arr} <br>     ${arr[0]} <br>
      <hr>     ${li} <br>     ${li[0]} <br>     ${li.get(0)} <br>
      <hr>     ${ma} <br>     ${ma["CN"]} <br>     ${ma.US} <br>
 
  </body> </html>
 
 
  [I@245c4786 1 [A, B, C] A A {CN=中国, IT=意大利, US=M国} 中国 M国
 
  |