Notas de estudio de la biblioteca de etiquetas JSTL (super detalladas)

El nombre completo de la biblioteca de etiquetas JSTL se refiere a la biblioteca de etiquetas estándar JSP. Es una biblioteca de etiquetas JSP de código abierto que mejora constantemente.

La expresión EL es principalmente para reemplazar el script de expresión en el jsp, y la biblioteca de etiquetas es para reemplazar el script de código. Esto hace que toda la página jsp sea mejor y más concisa.

JSTL consta de cinco bibliotecas de etiquetas con diferentes funciones.

Rango de función ODIO Prefijo
Principales características de la biblioteca de etiquetas http://java.sun.com/jsp/jstl/core C
formato http://java.sun.com/jsp/jstl/fmt fmt
función http://java.sun.com/jsp/jstl/functions fn
Base de datos (no utilizada) http://java.sun.com/jsp/jstl/sql sql
XML (no utilizado) http://java.sun.com/jsp/jstl/xml X

Utilice la instrucción taglib para introducir la biblioteca de etiquetas en la biblioteca de etiquetas jsp

Biblioteca de etiquetas CORE

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

Biblioteca de etiquetas XML

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

Biblioteca de etiquetas FMT

<% @ taglib prefix = “fmt” uri = “http://java.sun.com/jsp/jstl/fmt”%>

Biblioteca de etiquetas SQL

<% @ taglib prefix = “sql” uri = “http://java.sun.com/jsp/jstl/sql”%>

Biblioteca de etiquetas FUNCTIONS

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

1. Pasos para utilizar la biblioteca de etiquetas JSTL

1. Primero importe el paquete jar de la biblioteca de etiquetas jstl.

Taglibs-standard-impl-1.2.1.jar

Taglibs-standard-spec-1.2.1.jar

2. En el segundo paso, use la instrucción taglib para presentar la biblioteca de etiquetas.

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

2.uso de la biblioteca central

2.1 <c: set /> (rara vez se usa)

Rol: la etiqueta configurada puede guardar datos en el dominio

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

    域对象.setAttribute(key,value);

    scope 属性设置保存到哪个域
        page 表示 PageContext 域(默认值)
        request 表示 Request 域
        session 表示 Session 域
        application 表示 ServletContext 域

    var属性设置key是多少

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

Mostrar resultados:
Inserte la descripción de la imagen aquí

2.2 <c: si />

La etiqueta if se utiliza para realizar juicios if.

<%--
<c:if />
    if 标签用来做 if 判断。
    test 属性表示判断的条件(使用 EL 表达式输出)
--%>
<c:if test="${ 12 == 12 }">
    <h1>12 等于 12</h1>
</c:if>
<c:if test="${ 12 != 12 }">
    <h1>12 不等于 12</h1>
</c:if>

Mostrar resultados:

Inserte la descripción de la imagen aquí

2.3 <c: elija> <c: cuando> <c: en caso contrario> 标签

Rol: Juicio multidireccional. Muy cerca de cambiar ... caso ... predeterminado

<%--
    <c:choose> <c:when> <c:otherwise>标签
    作用:多路判断。跟 switch ... case .... default 非常接近

    choose 标签开始选择判断
    when 标签表示每一种判断情况
    test 属性表示当前这种判断情况的值
    otherwise 标签表示剩下的情况

    <c:choose> <c:when> <c:otherwise>标签使用时需要注意的点:
    1、标签里不能使用 html 注释,要使用 jsp 注释
    2、when 标签的父标签一定要是 choose 标签
--%>
<%
    request.setAttribute("height", 180);
%>
<c:choose>
    <%-- 这是 html 注释 --%>
    <c:when test="${ requestScope.height > 190 }">
        <h2>小巨人</h2>
    </c:when>
    <c:when test="${ requestScope.height > 180 }">
        <h2>很高</h2>
    </c:when>
    <c:when test="${ requestScope.height > 170 }">
        <h2>还可以</h2>
    </c:when>
    <c:otherwise>
        <c:choose>
            <c:when test="${requestScope.height > 160}">
                <h3>大于 160</h3>
            </c:when>
            <c:when test="${requestScope.height > 150}">
                <h3>大于 150</h3>
            </c:when>
            <c:when test="${requestScope.height > 140}">
                <h3>大于 140</h3>
            </c:when>
            <c:otherwise>
                其他小于 140
            </c:otherwise>
        </c:choose>
    </c:otherwise>
</c:choose>

Mostrar resultados:

Inserte la descripción de la imagen aquí

2.4 <c: forEach />

Rol: uso de salida transversal.

2.4.1 Travesía 1 a 10, salida

<c:forEach begin="1" end="10" var="i">
    <%--1. --%>
    ${i} <br>
    <%--2. --%>
    <h1>${i}</h1>
</c:forEach>

o:

<%--
<c:forEach />
作用:遍历输出使用
--%>
<%--
1.遍历 1 到 10,输出
    begin 属性设置开始的索引
    end 属性设置结束的索引
    var 属性表示循环的变量(也是当前正在遍历到的数据)
    
    for (int i = 1; i < 10; i++)
--%>
<table border="1">
    <c:forEach begin="1" end="10" var="i">
        <tr>
            <td>第${i}行</td>
        </tr>
    </c:forEach>
</table>

2.4.2 Matriz de objetos poligonales

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

2.4.3 Atravesar la colección de mapas

<%--遍历 Map 集合--%>
<%
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    // for ( Map.Entry<String,Object> entry : map.entrySet()) {
    // }
    request.setAttribute("map", map);
%>
<c:forEach items="${ requestScope.map }" var="entry">
    <%--输出所有的键值对--%>
    ${entry}
    <%--只输出我要的值--%>
    ${entry.key}
    ${entry.value}
    <%--按照自己希望的格式输出 key 和 value--%>
    <h1>${entry.key} = ${entry.value}</h1>
</c:forEach>

2.4.4 Recorra la colección de la lista: la clase del estudiante se almacena en la lista, con atributos: número, nombre de usuario, contraseña, edad, información del teléfono

<%--4.遍历 List 集合---list 中存放 Student 类,有属性:编号,用户名,密码,年龄,电话信息--%>
<%
    List<Student> studentList = new ArrayList<Student>();
    for (int i = 1; i <= 10; i++) {
        studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i));
    }
    request.setAttribute("stus", studentList);
%>
<table>
    <tr>
        <th>编号</th>
        <th>用户名</th>
        <th>密码</th>
        <th>年龄</th>
        <th>电话</th>
        <th>操作</th>
    </tr>
    <%--
        items 表示遍历的集合
        var 表示遍历到的数据
        begin 表示遍历的开始索引值
        end 表示结束的索引值
        step 属性表示遍历的步长值
        varStatus 属性表示当前遍历到的数据的状态
    for(int i = 1; i < 10; i+=2)
    --%>
    <c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
        <tr>
            <td>${stu.id}</td>
            <td>${stu.username}</td>
            <td>${stu.password}</td>
            <td>${stu.age}</td>
            <td>${stu.phone}</td>
            <td>${status.step}</td>
        </tr>
    </c:forEach>
</table>

Supongo que te gusta

Origin blog.csdn.net/weixin_45024585/article/details/112747979
Recomendado
Clasificación