##  EL表达式

1、概念:Expression  alnguage  表达式语言

2、作用:替换和简化JSP页面中java代码的编写

3、语法:${表达式}

4、注意:

  *  jsp默认支持EL表达式。如果要忽略EL表达式

    1、设置jsp中,page指令中的isELIgnored="true",忽略当前jsp页面中所有的EL表达式

    2、\${表达式}:忽略当前这个EL表达式

5、使用:

  1、运算

    *  运算符:

      1、算数运算符:+  -  *  /(div)  %(mod)

      2、比较运算符:>  <  >=  <=  ==  !=

      3、逻辑运算符:&&(and)  ||(or)  !(not)

      4、空运算符:empty

      *  功能:用于判断字符串、集合、数组对象是否为null或者长度是否为0

      *  ${empty  list}:用于判断字符串、集合、数组对象是否为null或者长度是否为0

      *  ${not  empty  str}:判断字符串,集合,数组对象是否不为null,且长度>0

  2、获取值

    1、EL表达式只能从域对象中获取值

    2、语法:

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

      *  域名称:(从小到大)

        1、pageScope:--->pageContext

        2、requestScope --->request

        3、sessionScope--->session

        4、applicationScope--->application(ServletContext)

      *  举例:在request域中存储了name=张三

      *  获取:${requestScope.name}

      2、${键名}:标识依次从最小的域中查找,是否有该键对应的值,知道找到为止。

      3、获取对象,List集合、Map集合的值

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

          *  本质上会去调用getter方法

package com.ftj.domain;

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

/**
 * 用户实体类javaBean
 */
public class User {
    private int id;
    private String username;
    private String password;
    private String gender;
    private Date birthday;

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }


    public User() {
    }

    public User(int id, String username, String password, String gender) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.gender = gender;
    }

    /**
     * 逻辑视图
     * @return
     */
    //格式化时间
    public String getBirth() {
        if (birthday != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            return sdf.format(birthday);
        }
        return "";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHehe() {
        return gender;
    }

    public void setHehe(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}
<%@ page import="com.ftj.domain.User" %>
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: 21seu.ftj
  Date: 2020/3/29
  Time: 12:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    User user = new User();
    user.setUsername("ftj");
    user.setPassword("123456");
    user.setBirthday(new Date());
    request.setAttribute("u",user);
%>

${requestScope.u.username}
${requestScope.u.password}
${u.birth}

</body>
</html>

        2、List集合:${域名城.键名[索引]}

        3、Map集合:

          *  ${域名城.键名.key名称}

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

<%@ page import="com.ftj.domain.User" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: 21seu.ftj
  Date: 2020/3/29
  Time: 12:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    User user = new User();
    user.setUsername("ftj");
    user.setPassword("123456");
    user.setBirthday(new Date());
    request.setAttribute("u",user);

    List list = new ArrayList<>();
    list.add("aaa");
    list.add("bbb");
    list.add(user);
    request.setAttribute("list",list);

    HashMap map = new HashMap<>();
    map.put("sname","李四");
    map.put("gendor","男");
    request.setAttribute("map",map);

%>

${requestScope.u.username}
${requestScope.u.password}
${u.birth}
<hr>

${list}
${list[2].username}
<hr>

${map}
${map.gendor}
${map["gendor"]}
</body>
</html>

  3、隐式对象:

    *  el表达式中有11个隐式对象

    *  pageContext:

      1、获取jsp其他8个内置对象

<%--
  Created by IntelliJ IDEA.
  User: 21seu.ftj
  Date: 2020/3/29
  Time: 13:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${pageContext.request}
<%--jsp页面中动态获取虚拟目录--%>
${pageContext.request.contextPath}
</body>
</html>

 

    

Posted on 2020-03-29 13:26  夏洛蒂的手稿  阅读(192)  评论(0编辑  收藏  举报