EL详解

Mr.he...大约 3 分钟JavaWEBEL

1. 概念

Expression Language(表达式语言)。

2. 作用

替换和简化jsp页面中java代码的编写

3. 语法格式

格式为:${表达式}

jsp默认支持el表达式的,如果要忽略el表达式:

  1. 设置jsppage指令中:isELIgnored="true"忽略当前jsp页面中所有的el表达式;
  2. \\${表达式} :忽略当前这个el表达式。

4. 使用

4.1. 用于运算

包括算数运算符、比较运算符、逻辑运算符、空运算符。具体如下:

  1. 算数运算符:+ - * /(div) %(mod)
  2. 比较运算符: > < >= <= == !=
  3. 逻辑运算符:&&(and) ||(or) !(not)
  4. 空运算符: empty

用于判断字符串、集合、数组对象是否为null或者长度是否为0。解析如下:

  • ${empty list}:表示判断字符串、集合、数组对象是否为null或者长度为0;
  • ${not empty str}:表示判断字符串、集合、数组对象是否不为null并且 长度>0。

4.2. 获取域对象中的值

el表达式只能从域对象中获取值。具体如下:

  1. ${域名称.键名}:从指定域中获取指定键的值

    • 域名称:

      1. pageScope    --> pageContext
      2. requestScope   --> request
      3. sessionScope   --> session
      4. applicationScope   --> application(ServletContext)
    • 示例:在request域中存储了name=张三;获取:${requestScope.name}

  2. ${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。

例:${name},先到pageContext中找是否有name,再到request中去找,直到找到为止。

上述的域名称顺序也是默认查找的顺序。

4.3. 获取对象、List集合、Map集合的值

可获取对象、List集合、Map集合的值,具体如下:

  1. 对象:${域名称.键名.属性名}

本质上会去调用对象的getter方法

  1. List集合:${域名称.键名[索引]}

  2. Map集合:

    • ${域名称.键名.key名称}
    • ${域名称.键名["key名称"]

代码演示如下:

  1. User类型的JavaBean:
package cn.itcast.domain;

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

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

    public User(String name, int age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public User() {
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
  1. JSP中EL表达式的使用:
<%@ 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>
    <%	//1.设置对象
        User user = new User();//获取JavaBean的User对象
    	//设置user对象的值
        user.setName("张三");
        user.setAge(23);
        user.setBirthday(new Date());
        request.setAttribute("u",user);//共享数据
		//2.设置List集合
        List list = new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add(user);
        request.setAttribute("list",list);
		//3.设置Map集合
        Map map = new HashMap();
        map.put("sname","李四");
        map.put("gender","男");
        map.put("user",user);
        request.setAttribute("map",map);
    %>

    	<h3>1.el获取对象中的值</h3>
	<!--
        通过的是对象中属性来获取。
        setter或getter方法,去掉set或get,在将剩余部分,首字母变为小写。
        过程:setName -- Name -- name
	-->
        ${requestScope.u.name}<br>
    	<!-- 通过默认顺序查找 -->
        ${u.age}<br>
        ${u.birthday}<br>
        ${u.birthday.month}<br><!-- u.birthday是Date类型,可以调用GetMonth方法返回月份 -->
    	${u.birStr}<br>

        <h3>2.el获取List集合中的值</h3>
        ${list}<br>
        ${list[0]}<br>
        ${list[1]}<br>
        ${list[10]}<br>
    	${list[2].name}<!-- list[2]存储的User类型的user,可以调用GetName方法返回姓名-->

        <h3>3.el获取Map集合中的值</h3>
        ${map.gender}<br>
        ${map["gender"]}<br>
        ${map.user.name}<!--map.user存储的Uesr类型的user,可以调用GetName方法返回姓名-->
</body>
</html>

5. 隐式对象

5.1. pageContext

用于获取jsp其他八个内置对象。常用的有${pageContext.request.contextPath}:动态获取虚拟目录。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el隐式对象</title>
</head>
<body>

    ${pageContext.request}<br><!--获取request对象-->
    ${pageContext.request.contextPath}<!--pageContext获取request对象后,使用getContextpath()方法实现在jsp页面动态获取虚拟目录-->
    <h4>动态获取虚拟目录</h4>
    <from action = "${pageContext.request.contextPath}/loginServlet">
        使用示例
    </from>
<%
    response.sendRedirect(request.getContextPath()+"资源路径");//java代码重定向动态获取虚拟目录

%>
</body>
</html>

el表达式中有11个隐式对象,将在后面进行学习。