jsp--JSTL标签库

目录

1.JSTL标签库介绍

2.JSTL 标签库的使用步骤

3.core核心库使用

3.1  <c:set>

3.2 <c:if />

3.3 <c:choose> <c:when> <c:otherwise >标签

3.4 <c:forEach />


1.JSTL标签库介绍

JSTL 标签库,全称是指 JSP Standard Tag Library   jsp标准标签库。是一个不断完善的开源代码jsp标签库。

EL表达式主要是为了替换jsp中的表达式脚本,而标签库则是为了替换代码脚本,这样使得整个jsp页面变得更加简洁。

JSTL 由五个不同功能的标签库组成 

 在jsp标签库中使用taglib指令引入标签库

CORE标签库

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

2.JSTL 标签库的使用步骤

1. 先导入jstl标签库的jar包;

2.使用taglib指令引入标签库;

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

3.core核心库使用

3.1  <c:set>

        作用:set 标签可以往域中保存数据

<body>
    <%-- <c:set />
        作用:set 标签可以往域中保存数据

        域对象.setAttribute(key,value);
        scope 属性设置保存到哪个域
        page 表示pageContext域 (默认值)
        request 表示Request域
        session 表示Session域
        application 表示ServletContext域

        key是多少
        value多少
        --%>
    保存之前:${requestScope.abc} <br/>
    <c:set scope="request"  var="abc" value="abcValue"></c:set>
    保存之后:${requestScope.abc} <br/>
</body>

输出结果:

保存之前:
保存之后:abcValue

3.2 <c:if />

if 标签用来做if判断。

     <%-- test属性表示判断的条件(使用EL表达式)    --%>
    <c:if test="${12 == 12}"> <h1> 12 == 12 </h1></c:if>
    <c:if test="${12 != 13}"> <h1> 12 != 13 </h1></c:if>

输出结果:

12==12

12!=13

3.3  <c:choose> <c:when> <c:otherwise > 标签

作用:多路判断。跟switch case default 非常接近。

注意点:

        1.标签里不能使用html注释;要使用jsp注释;

        2.when标签的父标签一定要是choose标签;

<%-- choose标签开始选择判断,
        when标签表示每一种判断情况
        test 属性表示当前这种情况判断的值
        otherwise 表示剩下的情况       --%>
    <%
        request.setAttribute("height",168);
    %>
    <c:choose>
        <c:when test="${requestScope.height > 190 }"><h3>小巨人</h3></c:when>
        <c:when test="${requestScope.height > 180 }"><h3>很高</h3></c:when>
        <c:when test="${requestScope.height > 170 }"><h3>还可以</h3></c:when>
        <c:otherwise>剩下小于170</c:otherwise>
    </c:choose>

3.4 <c:forEach />

作用:遍历输出使用。

        1.  遍历1 到 10 的输出;

<body>
    <%-- 1.遍历1到10,输出    --%>
    <%-- begin 属性设置开始的索引
       end 属性设置结束的索引
       var 属性表示循环的变量(也是当前正在遍历到的数据)
       --%>
    <c:forEach begin="1" end="10" var="i"><h1>第${i}行<br/><h1></c:forEach>
</body>

        2. 遍历Object数组

 <%-- 2. 遍历Object数组       --%>
    <%-- items 表示遍历的数据源(遍历的集合)
         var 表示当前遍历到的数据  --%>
    <%
        request.setAttribute("arr",new String[]{"10086","10001","10010"});
    %>
    <c:forEach items="${requestScope.arr}" var="item">${item}<br/></c:forEach>

        3.遍历map集合

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

        4. 遍历集合

<%--4.遍历list集合    --%>
    <%--  --%>
    <%
        List<Student> list = new ArrayList<Student>();
        for(int i =1;i<=10;i++){
            list.add(new Student(i,"username"+i,"password"+i,i,"phone"+i));
        }
        request.setAttribute("studentList",list);
    %>
    <table>
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>年龄</th>
            <th>电话</th>
            <th>操作</th>
        </tr>
            <c:forEach items="${requestScope.studentList}" var="student">
                <tr>
                    <td>${student.id}</td>
                    <td> ${student.username}</td>
                    <td> ${student.password}</td>
                    <td> ${student.age}</td>
                    <td>${student.phone}</td>
                    <td>修改,删除</td>
                </tr>
            </c:forEach>

    </table>

集合遍历中标签属性:

<%-- items 表示遍历的集合
       var 表示遍历到的数据
       begin表示遍历的开始索引值
       end 表示结束的索引值
       step属性表示遍历的步长值
       --%>
<table>
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>年龄</th>
            <th>电话</th>
            <th>操作</th>
        </tr>
            <%-- items 表示遍历的集合
                   var 表示遍历到的数据
                   begin表示遍历的开始索引值
                   end 表示结束的索引值
                   step属性表示遍历的步长值
                   --%>
            <c:forEach begin="" end="8" step="2" varStatus="status" items="${requestScope.studentList}" var="student">
                <tr>
                    <td>${student.id}</td>
                    <td> ${student.username}</td>
                    <td> ${student.password}</td>
                    <td> ${student.age}</td>
                    <td>${student.phone}</td>
                    <td>修改,删除</td>
                    <td>${status.index}</td>
                </tr>
            </c:forEach>

    </table>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值