05- EL和 JSTL 标签库小知识点整理

本文介绍EL表达式的使用方法及JSTL标签库的功能,包括全域查找、JavaBean导航、内置对象等内容,并详细讲解了核心标签库中的out、set、if等标签的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时隔几天,又开始整理一下了,本文章视频来自于 https://edu.aliyun.com/roadmap/java ,笔记为自己所写, 内容比较简单,但容易忘掉,记录下来,希望这里能对朋友们有所帮助叭。

19- EL 表达式

  • jsp 内置语言;

全域查找:

  • pageContext. setAttribute(“xxx”,“pageContext”);
  • request. setAttribute(“xxx”,“request”);
  • session. setAttribute(“xxx”,“session”);
  • application. setAttribute(“xxx”,“application”);
${ xxx }// 分别显示以上的四个域内的value;
${ requestScope.xxx } // 查找指定域内的value;注意,必须 + Scope后缀;
  • JavaBean 导航

11 个内置对象:

  • EL 替代:<%=…%>, 输出

    • 前十个map
    • pageScope
    • requestScope
    • sessionScope
    • applicationScope
    • param
      • param. name( single ) & paramValues. names[0]( arrays )
    • paramValues
    • header
      • 对应请求头,key头名称,value头值,
    • headerValues
    • initParam
      • <context-param>
    • cookie
    • pageContext
      • ${pageContext.request.contextPath }项目路径;

    EL函数库:

<% taglib prefix="fn" uri="http://java.sum.com/jsp/jstl/functions" %>
<h1>
    the EL function:
</h1>
${fn:length(array)}

20- JSTL 标签库

  • apache 提供,依赖于 EL
  • 四大库:
    • core:核心标签库:tag–》c
      • out & set:out:解决是否转移比如:(’>’)
      • remove
      • url
        • value: 在路径前面加上项目名;
        • 加上param:提供参数;
        • <c:url value="/index. jsp"> <c:param name=“username” value=“Jason”/> </ c:url>
    • 逻辑标签:
      • if
        • <c:if test=“boolean”>…</c :if>
      • choose
        • <c:choose >
        • <c:when test=“boolean1”> … </c:when >
        • <c:when test=“boolean2”> … </c:when >
        • <c:otherwise >… </c:otherwise >
        • </c:choose >
      • forEach
    • fmt:格式化,format,日期,数字
    • sql
    • xml
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2021/7/1
  Time: 14:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>choose</title>
</head>
<body>
<h1>taglib</h1>
<hr>
<c:if test="${empty param.name}">
    <div><% out.print("tag if,the param.name empty;"); %></div>
</c:if>
<c:choose>
    <c:when test="${empty param.name}">
        <div><% out.print("tag choose.when the param.name empty;"); %></div>
    </c:when>
    <c:otherwise>
        <div><% out.print("tag choose.when the param.name not empty:"); %>
            <br>
            ${param.name}
        </div>
    </c:otherwise>
</c:choose>
</body>
</html>

在这里插入图片描述

forEach

<%--    forEach 中,E 大写,不是 小写的e,我裂开--%>
    <c:forEach items="${ applicationScope.map }" var="entry">
        <tr>
            <td>${ entry.key }</td>
            <td>${ entry.value }</td>
        </tr>
    </c:forEach>

//

<c:forEach var="item" begin="1" end="10" step="2">// step 默认为1;
    ${item}<br/>
</c:forEach>
the arr: <br>
<% String[] arr = { "hello","world" };
    request.setAttribute("arr",arr);
%>
增强for:<br>
<c:forEach items="${arr}" var="item">// step 默认为1;
    ${item}<br/>
</c:forEach>

the show:


循环状态变量:

varStatus:循环状态变量;==to: 属性:count:循环元素个数,index:下标,first:是否为第一个元素,last:是否为最后一个元素,current:是否为当前元素;

fmt


在这里插入图片描述


<h1>fmt:</h1>
<%
    Date date = new Date();
    pageContext.setAttribute("date",date);
    request.setAttribute("number",3.1415926);
%>
<fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH:mm:ss" />
<br>
the number:
<fmt:formatNumber value="${requestScope.number}" pattern="0.000"/><!--四舍五入-->
<fmt:formatNumber value="${requestScope.number}" pattern="#.###"/><!--区别0.00不会补位-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

J.CH.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值