侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 744 篇文章
  • 累计创建 64 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

JavaWeb(10)之EL表达式及11大隐式对象

zze
zze
2017-06-16 / 0 评论 / 0 点赞 / 737 阅读 / 4762 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

EL 表达式

EL 表达式的作用就是帮助我们简化 JSP 中的 Java 代码。

示例

1、在四大作用域中取值:

<!-- page.jsp -->
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    pageContext.setAttribute("str", "from pageContext");
    request.setAttribute("str", "from request");
    session.setAttribute("str", "from session");
    application.setAttribute("str", "from application");
%>

${pageScope.str} <br>
${requestScope.str} <br>
${sessionScope.str} <br>
${applicationScope.str} <br>
<%--在 el 表达式中不指定 scope 直接使用 name,会依次在 pageContext->request->session->application --%>
${str}
</body>
</html>

image.png
2、取 array 和 list 中的值:

<!-- page.jsp -->
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String[] array = {"a", "b", "c", "d"};
    pageContext.setAttribute("array", array);

    List list = new ArrayList();
    list.add(11);
    list.add(22);
    list.add(33);
    list.add(44);
    pageContext.setAttribute("list", list);
%>

${array[0]},${array[1]},${array[2]},${array[3]}
<hr/>
${list[0]},${list[1]},${list[2]},${list[3]}
</body>
</html>

image.png

3、取 map 中的值:

<!-- page.jsp -->
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String[] array = {"aa", "bb"};

    Map map = new HashMap();
    map.put("name1", "zhangsan");
    map.put("name2", "lisi");
    map.put("name3", "wangwu");
    map.put("name4", "zhaoliu");
    pageContext.setAttribute("map", map);
%>
${map["name1"]},${map["name2"]},${map.name3},${map.name4}
</body>
</html>

image.png
4、取对象属性的值:

// com.zze.bean.User
package com.zze.bean;

public class User {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
<!-- page.jsp -->
<%@ page import="com.zze.bean.User" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    User user = new User();
    user.setName("张三");
    user.setAge(18);

    pageContext.setAttribute("user", user);
%>
姓名:${pageScope.user.name} <br>
年龄:${user.age}
</body>
</html>

image.png

11 大隐式对象

隐式对象即在 EL 表达式中能直接使用的对象,有 11 个如下:

  • pageContext:就是 JSP 内置对象的 pageContext。
  • pageScope:当前 JSP 页的 Map 域。
  • requestScope:当前请求范围的 Map 域。
  • sessionScope:当前会话范围的 Map 域。
  • applicationScope:当前应用程序的 Map 域。

上面所说的 Map 域指的是使用 setAttribute 存放值存放到的 Map 实例。

  • header:请求头键和值的 Map。
  • headerValues:请求头键和值的 Map,值为数组结构。
  • param:请求参数的参数名和值的 Map。
  • paramValues:请求参数的参数名名和值的 Map,值为数组结构。
  • initParam:所有初始化参数的 Map(这里的初始化参数指的是全局初始化参数)。
  • cookie:所有 Cookie 的键和值 Map。
0

评论区