1,EL表达式
 
1.1 概述
EL(全称Expression Language)表达式语言,用于简化JSP页面内的Java代码。
EL表达式的主要作用是 获取数据 。其实就是从域对象中获取数据,然后将数据展示在页面上。
而EL表达式的语法也比较简单,${expression} 。例如:${brands}就是获取域中存储的key为brands的数据。
1.2 代码演示
-  定义 servlet,在servlet中封装一些数据并存储到request域对象中并转发到el-demo.jsp页面。package com.dcxuexi.web; import com.dcxuexi.pojo.Brand; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; /*** * @Title ServletDemo1 * @Description TOTD * @Auter DongChuang * @Date 2023/1/2 17:58 * @Version 1.0.0 */ @WebServlet("/demo1") public class ServletDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) 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域中 req.setAttribute("brands",brands); //3. 转发到 el-demo.jsp req.getRequestDispatcher("/el-demo.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }注意: 此处需要用转发,因为转发才可以使用 request对象作为域对象进行数据共享
-  在 el-demo.jsp中通过 EL表达式 获取数据<%-- Created by IntelliJ IDEA. User: DongChuang Date: 2023/1/2 Time: 18:02 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> ${brands} </body> </html>jsp页面中isELIgnored属性默认为true,它会将jsp页面的数据直接进行字符处理。因此需要设置 <%@ page isELIgnored="false"%>
-  在浏览器的地址栏输入 http://localhost:8080/jsp-demo/demo1,页面效果如下: 
1.3 域对象
JavaWeb中有四大域对象,分别是:
- page:当前页面有效
- request:当前请求有效
- session:当前会话有效
- application:当前应用有效
el表达式获取数据,会依次从这4个域中寻找,直到找到为止。而这四个域对象的作用范围如下图所示

例如:${brands},el表达式获取数据,会先从page域对象中获取数据,如果没有再到requet域对象中获取数据,如果再没有再到session域对象中获取,如果还没有才会到application中获取数据。
2,JSTL标签
 
2.1 概述
JSP标准标签库(Jsp Standarded Tag Library),使用标签取代JSP页面上的Java代码。如下代码就是JSTL标签
<c:if test="${flag == 1}">
    男
</c:if>
<c:if test="${flag == 2}">
    女
</c:if>
上面代码看起来是不是比JSP中嵌套Java代码看起来舒服好了。而且前端工程师对标签是特别敏感的,他们看到这段代码是能看懂的。
2.2 JSTL标签
 
2.2.1 核心标签
核心标签是最常用的JSTL标签。引用核心标签库的语法如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

2.2.2 格式化标签
JSTL格式化标签用来格式化并输出文本、日期、时间、数字。引用格式化标签库的语法如下:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

2.2.3 SQL标签
 
JSTL SQL标签库提供了与关系型数据库(Oracle,MySQL,SQL Server等等)进行交互的标签。引用SQL标签库的语法如下:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

2.2.4 XML标签
 
JSTL XML标签库提供了创建和操作XML文档的标签。引用XML标签库的语法如下:
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
在使用xml标签前,你必须将XML和XPath的相关包拷贝至你的<Tomcat 安装目录>\lib下:
-  XercesImpl.jar下载地址: http://www.apache.org/dist/xerces/j/
-  xalan.jar下载地址: http://xml.apache.org/xalan-j/index.html

2.2.5 JSTL函数
 
JSTL包含一系列标准函数,大部分是通用的字符串处理函数。引用JSTL函数库的语法如下:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

2.3 常用的标签
我们只对两个最常用的标签进行讲解,<c:forEach> 标签和 <c:if> 标签。
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>
-  在 JSP页面上引入JSTL标签库<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
-  使用标签 
2.3.1 if 标签
 
<c:if>:相当于if判断
- 属性:test,用于定义条件表达式
<c:if test="${flag == 1}">
    男
</c:if>
<c:if test="${flag == 2}">
    女
</c:if>
代码演示:
-  定义一个 servlet,在该servlet中向 request 域对象中添加 键是status,值为1的数据package com.dcxuexi.web; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /*** * @Title ServletDemo2 * @Description TOTD * @Auter DongChuang * @Date 2023/1/2 20:55 * @Version 1.0.0 */ @WebServlet("/demo2") public class ServletDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1. 存储到request域中 req.setAttribute("status",1); //2. 转发到 jstl-if.jsp req.getRequestDispatcher("/jstl-if.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
-  定义 jstl-if.jsp页面,在该页面使用<c:if>标签<%-- Created by IntelliJ IDEA. User: DongChuang Date: 2023/1/2 Time: 20:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page isELIgnored="false" %> <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>注意: 在该页面已经要引入 JSTL核心标签库<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2.3.1 forEach标签
 
<c:forEach>:相当于for循环。java中有增强for循环和普通for循环,JSTL中的 <c:forEach> 也有两种用法
2.3.1.1 用法一
类似于Java中的增强for循环。涉及到的 <c:forEach> 中的属性如下
-  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>
代码演示:
-  servlet还是使用之前的名为ServletDemo1。
-  定义名为 jstl-foreach.jsp页面,内容如下:<%-- Created by IntelliJ IDEA. User: DongChuang Date: 2023/1/2 Time: 21:35 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <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> <td>${brand.id}</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>
2.3.1.2 用法二
类似于Java中的普通for循环。涉及到的 <c:forEach> 中的属性如下
-  begin:开始数
-  end:结束数
-  step:步长
实例代码:
从0循环到10,变量名是 i ,每次自增1
<c:forEach begin="0" end="10" step="1" var="i">
    ${i}
</c:forEach>



















