Java && JSTL(标准标签库) 和 EL表达式

JSTL(标准标签库) 和 EL表达式

tagLib 技术的出现,就是为了解决 JSP页面中大量的难于维护的 Java 代码。

  • 根本目的就是避免在JSP页面中引入大量的Java代码。

JSTL java standard tag lib : Java的标准标签库(Commons 下面实现的)

EL表达式 是 JSP技术中原生支持的 表达式解析,只要遵循它的语法写就ok。

EL表达式

格式:

${表达式内容}

EL表达式支持独立在JSP中使用
记住:它有11大内置对象

  • pageScope
  • requestScope
  • sessionScope
  • applicationScope
  • param 一个请求参数值
  • paramValues 一组请求参数值
  • header 一个请求头值
  • headerValues 一组请求头值
  • initParam 初始化参数
  • cookie cookie值
  • pageContext JSP中的PageContext ,万金油对象。

属性访问(getXxx方法):

变量名.xxx
变量名['xxx']
变量名[“xxx”]

变量名 可以是 域对象中的任意 name 。 它会自动从下到大 查询不同的作用范围

获取项目名 : ${pageContext.request.contextPath}

表单回显: value=" p a r a m . x x x x " v a l u e = " {param .xxxx}" value=" param.xxxx"value="{任意域的name.属性访问}"

代码演示

<%@ page >contentType="text/html;charset=UTF-8" >language="java" %>
<html>
<head>
  <title>$Title$</title>
</head>
<body>
<%
   pageContext.setAttribute("pc", "pc1");
   request.setAttribute("rc", "rc1");
   session.setAttribute("sc", "sc1");
   application.setAttribute("ac", "ac1");
%>

<h1>pageScope</h1>
${pageScope.pc} >${pageScope['pc']}${pageScope["pc"]}
<h1>requestScope</h1>
${requestScope.rc}
<h1>sessionScope</h1>
${sessionScope['sc']}
<h1>applicationScope</h1>
${applicationScope.ac}
<h1>param 请求参数</h1>
${param.username}
${paramValues.hobby}
<h1>header 请求头</h1>
${header['User-Agent']}
<h1>cookie </h1>
${cookie['JSESSIONID'].value}
<h1>万金油的 pageContext  </h1>
<%--  以后我们写客户端路径时: 携带项目名 ,>使用EL表达式--%>
<%= request.getContextPath()%>
${pageContext.request.contextPath}
${pageContext.request.servletContext.contextP>ath}
${pageContext.servletContext.contextPath}
${pageContext.session.servletContext.contextP>ath}

<form action="><%=request.getContextPath()%>/abc.html"></form>
<form >action="${pageContext.request.contextPath}/ab>c.html"></form>

</body>

</html>

JSTL标签库

菜鸟教程JSP
下载、引入。在这里插入图片描述
.jar 包是我们普通Java项目打包的结果。

.war 包是我们的 Web项目打包的结果。

两者本质上都是压缩包。

引入lib目录下的两个jar包到我们的项目中:
在这里插入图片描述
在这里插入图片描述

使用步骤

  1. 在页面中使用 taglib 指令

    <%@ taglib prefix="c"uri="一个后缀为core的地址"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

  2. 直接在JSP页面中,就可以使用标签库提供的一些标签来表达动态的逻辑。在这里插入图片描述

c:if 条件判断

在这里插入图片描述

<%
    session.setAttribute("salary", 1000);
%>

<h1>c:if标签的使用</h1>
<c:if test="${salary>2000}">
    <p>我的薪水会被显示 ${salary}</p>
</c:if>

注意: jstl中没有包含 c:else 一类的标签,我们使用时,需要自己使用多个 c:if 来指定不同的条件。

c:choose 多条件选择

在这里插入图片描述

<h1>c:choose标签的使用</h1>
<c:choose>
    <c:when test="${day==1}">
        <span>星期日</span>
    </c:when>
    <c:when test="${day==2}">
        <span>星期一</span>
    </c:when>
    <c:when test="${day==3}">
        <span>星期二</span>
    </c:when>
    <c:when test="${day==4}">
        <span>星期三</span>
    </c:when>
    <c:when test="${day==5}">
        <span>星期四</span>
    </c:when>
    <c:when test="${day==6}">
        <span>星期五</span>
    </c:when>
    <c:when test="${day==7}">
        <span>星期六</span>
    </c:when>
    <c:otherwise>
        <span>瞎搞!</span>
    </c:otherwise>
</c:choose>

c:forEach 迭代标签

在这里插入图片描述
在这里插入图片描述

注意:items属性值一定要是一个 EL表达式 表示的集合对象。

<h1>c:forEach迭代标签[list/map]</h1>

<select>
    <c:forEach items="${sList}" var="item">
    <option>${item.concat("+")}</option>
    </c:forEach>
</select>

<ul>
    <c:forEach items="${sStudents}" var="s">
    <li>${s.name}---${s.age}</li>
    </c:forEach>
</ul>

<ol>
    <c:forEach items="${map}" var="entry" varStatus="vs" begin="1">
        <!--
            关于迭代状态:
                我们可以为 forEach 标签指定  varStatus 属性值来定义一个状态变量

                接下来,我们就可以通过状态变量访问一些迭代的信息:
                    first : 返回是否是第一项
                    last    : 返回是否是最后一项
                    index : 当前项的索引从0开始
                    count :1开始的第几项
                    current : 当前的迭代的项
                    step: 返回迭代的步长
                    begin: 返回迭代的起始索引
                    end:返回迭代的截止索引
        -->
<%--        <li>${entry.key}----${entry.value}  ||  ${entry.getKey()}----${entry.getValue()}</li>--%>
        <li>${vs.first} ${vs.last} ${vs.index} ${vs.count} ${vs.current} ${vs.begin} ${entry.key}----${entry.value}</li>
    </c:forEach>
</ol>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值