JSTL标签

JSTL标签

  • 如何使用

    1. 需要导入以下两个jar包

      在这里插入图片描述

    2. 在jsp页面中加入以下信息

      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
  • set标签

    用于在域对象中存储数据

            设置之前:${requestScope.str}<br>
            <%--
                set标签用于在域对象中存储数据
                scope表示在哪个域中存数据
                var相当于key值
                value相当于value值
            --%>
            <c:set scope="request" var="str" value="helloworld"/>
            设置之后:${requestScope.str}<br>
    
  • if标签

            <%-- 
               test中写EL表达式的判断语句
            --%>
            <c:if test="${12 == 12}">
                <h3>这是对的</h3>
            </c:if>
    
  • choose、when、otherwise标签

    相当于Java中的switch…case…default

    有一个注意点:when的上一级,必须是choose

            <c:set scope="request" var="grade" value="80"/>
            <c:choose>
                <c:when test="${requestScope.grade == 100}">
                    <h3>太棒啦</h3>
                </c:when>
                <c:when test="${requestScope.grade > 90}">
                    <h3>非常好</h3>
                </c:when>
                <c:when test="${requestScope.grade >80}">
                    <h3>还不错</h3>
                </c:when>
                <c:when test="${requestScope.grade > 60}">
                    <h3>再接再厉</h3>
                </c:when>
                <c:otherwise>
                    <h3>要努力哦</h3>
                </c:otherwise>
            </c:choose>
    
  • foreach标签

    begin设置i的起始值

    end设置i的终止值

    step代表步长

    var代表当前的数值

    items表示表示遍历的集合

    varStatus表示当前遍历到的数据的状态

    • 遍历1-10的数

      	<%--遍历1-10
              for (int i = 1; i <= 10; i++) {
                  begin设置i的起始值
                  end设置i的终止值
                  step代表步长 i++
                  var代表当前的数值
              }
          --%>
          <c:forEach begin="1" end="10" step="1" var="i">
              ${i}<br>
          </c:forEach>
      
    • 遍历Object数组

          <%--items设置数组对象,var代表当前遍历到哪个对象    --%>
          <%
              Object[] arr = new Object[]{"hello",new Date(),123};
              request.setAttribute("arr",arr);
          %>
          <c:forEach items="${requestScope.arr}" var="item">
              ${item}<br>
          </c:forEach>
      
    • 遍历map集合

          <%
              Map<String,String> map = new HashMap<>();
              map.put("key1","value1");
              map.put("key2","value2");
              map.put("key3","value3");
              request.setAttribute("map",map);
          %>
          <c:forEach items="${requestScope.map}" var="item">
              ${item.key} ==== ${item.value} <br>
          </c:forEach>
      
    • 遍历list集合

          <%
              List<String> list = new ArrayList<>();
              list.add("北京");
              list.add("浙江");
              list.add("上海");
              request.setAttribute("list",list);
          %>
          <c:forEach items="${requestScope.list}" var="item">
              ${item}<br>
          </c:forEach>
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值