• EL 表达式


    EL 表达式

    概述

    EL(全称Expression Language)表达式语言,用于简化JSP页面内的java代码。EL表达式的主要作用是获取数据。其实就是从域对象中获取数据,然后将数据,然后将数据展示在页面上。
    而EL表达时候的语法也比较简单,$ {expression}.例如:$ {brands} 就是获取域中存储的key为brands的数据。

    代码演示

    • 定义servlet,在servlet中封装一些数据并转发到el-demo.jsp页面。
    @WebServlet("/demo1")
    public class ServletDemo1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1. 准备数据
            List<Brand> brands = new ArrayList<Brand>();
            brands.add(new Brand(1,"三只松鼠","三只松鼠",100,"三只松鼠,好吃不上火",1));
            brands.add(new Brand(2,"优衣库","优衣库",200,"优衣库,服适人生",0));
            brands.add(new Brand(3,"小米","小米科技有限公司",1000,"为发烧而生",1));
    
            //2. 存储到request域中
            request.setAttribute("brands",brands);
    
            //3. 转发到 el-demo.jsp
            request.getRequestDispatcher("/el-demo.jsp").forward(request,response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    注意: 此处需要用转发,因为转发才可以使用 request 对象作为域对象进行数据共享

    • el-demo.jsp 中通过 EL表达式 获取数据
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        ${brands}
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    域对象

    javaweb中有四大域对象,分别是:

    • page:当前页面有效
    • request:当前请求有效
    • session:当前会话有效
    • application:当前应用有效
      el表达式获取数据,会依次从这四个域中寻找,直到找到为止。而这四个域对象的作用范围如下图:
      在这里插入图片描述

    例如: ${brands},el 表达式获取数据,会先从page域对象中获取数据,如果没有再到 requet 域对象中获取数据,如果再没有再到 session 域对象中获取,如果还没有才会到 application 中获取数据。

    JSTL标签

    概述

    JSP标准标签库(JSP Standard Tag Library),使用标签取代JSP页面上的java代码。如下代码就是JSTL标签

    <c:if test="${flag == 1}"></c:if>
    <c:if test="${flag == 2}"></c:if>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    上面代码看起来是不是比JSP中嵌套java代码看起来舒服多了。而且前端工程师对标签是特别敏感的,它们看到这段代码是能看懂的。
    JSTL提供了很多标签,如图
    在这里插入图片描述

    我们只对两个最常用的标签进行讲解, 标签和 标签。
    JSTL使用也是比较简单的。分为如下步骤:

    • 导入坐标
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 在JSP页面上引用JSTL标签库
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    • 使用标签

    if标签

    :相当于 if 判断

    • 属性:test,用于定义条件表达式
    <c:if test="${flag == 1}"></c:if>
    <c:if test="${flag == 2}"></c:if>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    代码演示:

    • 定义一个 servlet ,在该 servlet 中向 request 域对象中添加 键是 status ,值为 1 的数据
    @WebServlet("/demo2")
    public class ServletDemo2 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1. 存储数据到request域中
            request.setAttribute("status",1);
    
            //2. 转发到 jstl-if.jsp
            数据request.getRequestDispatcher("/jstl-if.jsp").forward(request,response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 定义 jstl-if.jsp 页面,在该页面使用 标签
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <%--
            c:if:来完成逻辑判断,替换java  if else
        --%>
        <c:if test="${status ==1}">
            启用
        </c:if>
    
        <c:if test="${status ==0}">
            禁用
        </c:if>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ==注意:==在该页面已经要引入JSTL核心标签库
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

    forEach 标签

    :相当于 for 循环。java中有增强for循环和普通for循环,JSTL 中的 也有两种用法

    用法一

    类似于 Java 中的增强for循环。涉及到的 中的属性如下

    • items:被遍历的容器

    • var:遍历产生的临时变量

    • varStatus:遍历状态对象

    如下代码,是从域对象中获取名为 brands 数据,该数据是一个集合;遍历遍历,并给该集合中的每一个元素起名为 brand,是 Brand对象。在循环里面使用 EL表达式获取每一个Brand对象的属性值

    <c:forEach items="${brands}" var="brand">
        <tr align="center">
            <td>${brand.id}</td>
            <td>${brand.brandName}</td>
            <td>${brand.companyName}</td>
            <td>${brand.description}</td>
        </tr>
    </c:forEach>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ** 代码演示:**

    • servlet 还是使用之前的名为 ServletDemo1

    • 定义名为 jstl-foreach.jsp 页面,内容如下:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <input type="button" value="新增"><br>
    <hr>
    <table border="1" cellspacing="0" width="800">
        <tr>
            <th>序号</th>
            <th>品牌名称</th>
            <th>企业名称</th>
            <th>排序</th>
            <th>品牌介绍</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
    
        <c:forEach items="${brands}" var="brand" varStatus="status">
            <tr align="center">
                <%--<td>${brand.id}</td>--%>
                <td>${status.count}</td>
                <td>${brand.brandName}</td>
                <td>${brand.companyName}</td>
                <td>${brand.ordered}</td>
                <td>${brand.description}</td>
                <c:if test="${brand.status == 1}">
                    <td>启用</td>
                </c:if>
                <c:if test="${brand.status != 1}">
                    <td>禁用</td>
                </c:if>
                <td><a href="#">修改</a> <a href="#">删除</a></td>
            </tr>
        </c:forEach>
    </table>
    </body>
    </html>
    
    • 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

    varStatus 的属性:
    index 属性:当前迭代的元素在集合中的索引,从 0 开始。
    count 属性:当前迭代的元素是集合中第几个元素,从 1 开始。例如,如果当前迭代的元素索引为 3,那么它是集合中第 4 个元素,即 count 的值为 4。
    first 属性:当前迭代的元素是否为集合中的第一个元素。
    last 属性:当前迭代的元素是否为集合中的最后一个元素。
    current 属性:当前迭代的元素。注意,该属性的值和 var 属性的值代表集合中的同一个元素。

    用法二

    类似于 Java 中的普通for循环。涉及到的 中的属性如下

    • begin:开始数

    • end:结束数

    • step:步长

    实例代码:

    从0循环到10,变量名是 i ,每次自增1

    <c:forEach begin="0" end="10" step="1" var="i">
        ${i}
    </c:forEach>
    
    • 1
    • 2
    • 3

  • 相关阅读:
    土耳其商务团一行莅临优积科技考察交流
    我为什么不喜欢关电脑?
    ObjectARX简单自定义实体的实现
    GBase 8s CLOSE 语句
    基于JAVA汽车销售系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    C. Good String(暴力枚举)
    抽取泛微和建云的销售合同定时任务(要求记录翻译不成功的字段)
    USB3.0:VL817Q7-C0的LAYOUT指南(三)
    C#:最少硬币问题算法​(附完整源码)
    Java五种设计模式实现奶茶订单生成系统小DEMO
  • 原文地址:https://blog.csdn.net/qq_53037676/article/details/126048566