zoukankan      html  css  js  c++  java
  • JavaWeb 之 JSTL 标签库

    一、JSTL 标签库概述

      1、概念

        JSTL标签库,全称是 JSP Standard Tag Library  JSP标准标签库。

        是由 Apache 组织提供的开源的、免费的、不断完善的 JSP 标签。

      2、作用

        JSTL 标签库是用来简化和替换 JSP 页面上的 java 代码(代码脚本),这样使得整个 JSP 页面变得更佳简洁。

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

        

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

    CORE 标签库:     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    XML 标签库:      <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
    FMT 标签库:      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    SQL 标签库:      <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
    FUNCTIONS 标签库:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    

      

    二、使用步骤

      1、导入 jstl 相关的jar包

        

      2、引入标签库

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

      3、使用标签

        使用 taglib 指令引入标签库,就可以使用标签了,其中 prefix 用来指定前缀名,可以改变,但是建议使用约定俗成的比较好。

        URI 相当于库的唯一标识,因为 JSTL 由多个不同的库组成,使用该属性指定要导入哪个库。

    三、常用的 JSTL 标签

      1、<c:out> 标签

        作用:用于计算一个表达式并将结果输出到当前页面

        语法:

    格式:<c:out value="" default="" escapeXml=""></:out>

    属性说明: ① value:Object 类型,表示要输出的值;

          ② default:object 类型,当 value 为 null 时显示的默认值;

          ③ escaXml:boolean 类型,是否对特殊字符进行转义;

        Demo:

    <c:out value="${user.name}" default="" escapeXml="true"></c:out>

      2、<c:set /> 标签

        作用: set 标签用于添加或修改域中的数据

        语法:

    添加数据格式: <c:set  scope="" var=""  value="" />

    属性说明: ① scope:该属性设置将数据保存到那个域中(四个作用域,);

              page 表示 PageContext 域(默认值);request 表示 Request 域;session 表示 Session 域;application 表示 ServletContext 域

          ② val :该属性设置 key 是什么,表示域中存放的属性名;

          ③ value :设置属性的值;

    修改数据格式: <c:set property=""  target=""  value=""></c:set>

    属性说明: ①  property :表示指定要修改的对象的属性名;

          ② target:表示要修改的域对象的属性吗(必须是 JavaBean 或 Map);

          ③ value:设置属性的值; 

        Demo:

     1   <%-- 
     2        <c:set />
     3        作用: set 标签可以往域中保存数据
     4        域对象.setAttribute(key,value);
     5              scope 属性设置保存到哪个域
     6                    page 表示 PageContext 域(默认值)
     7                    request 表示 Request 域
     8                    session 表示 Session 域
     9                    application 表示 ServletContext 域
    10              var 属性设置 key 是多少
    11              value 属性设置值
    12              property:指定要修改的对象的属性名
    13              target:要修改的域对象的属性名
    14      --%>
    15      保存之前: ${ sessionScope.abc } <br>
    16      <c:set scope="session" var="abc" value="abcValue"/>
    17      保存之后: ${ sessionScope.abc } <br>
    18 
    19      <%--  <c:set> 修改属性  --%>
    20     <c:set property="name" target="${user}" value="张三"></c:set>        

      3、<c:remvoe>

        作用:用于移除域中的属性

        语法:

    格式:  <c:remove var="" scope="">

    参数说明:① value:设置要移除的属性的名字

         ② scope:设置要移除属性所在的域,若不指定则删除所有域中的对应属性

        Demo:

    1 移除所有域中key属性:<c:remove var="key"/>
    2 移除request中的key属性: <c:remove var="key" scope="request"/

      4、<c:if />  标签

        if 标签:用于实现 if 语句的判断功能,相当于 java 代码的 if 语句。

        语法:

    格式:<c:if test="表达式" />

    属性说明:test 必须属性,接收 boolean 类型的表达式(使用 EL 表达式输出)

            如果表达式为true,则显示 if 标签体内容,如果为false,则不显示标签体内容

        注意:<c:if /> 标签没有else情况,想要else情况,则可以在定义一个c:if标签

        Demo:

     1 // 往 request 域中存放数据
     2 request.setAttribute("number",4);
     3 // 使用 if 标签显示数据
     4 <c:if test="${number % 2 != 0}">
     5 
     6         ${number}为奇数
     7 
     8  </c:if>
     9 
    10 <c:if test="${number % 2 == 0}">
    11 
    12         ${number}为偶数
    13 
    14 </c:if>

      5、<c:choose> <c:when> <c:otherwise>标签

        作用:多路判断,与 java中的 switch.....case......default 非常类似

        语法:

    格式:<c:choose>

          <c:when test="">

            语句

          </c:when>

          <c:otherwise>

            语句

          </c:otherwise>

         </c:choose>

    说明:① choose 标签开始选择判断

       ② when 标签标示每一种判断情况,test 属性表示当前这种判断情况的值;

       ③ otherwise 标签表示剩下的其他情况

         注意:

          ① 标签里不能使用 HTML 注释,要使用 jsp 注释;

          ② when 标签的父标签一定要是 choose 标签;

          ③ 当 <c:when> 有一个满足后,就跳出了,不用 break,而 case 需要;

        Demo:

     1 <%
     2       request.setAttribute("height", 180);      //往域中存放数据
     3     %>
     4     <c:choose>
     5       <%-- 这是 html 注释 --%>
     6       <c:when test="${ requestScope.height > 190 }">
     7         <h2>小巨人</h2>
     8       </c:when>
     9       <c:when test="${ requestScope.height > 180 }">
    10         <h2>很高</h2>
    11       </c:when>
    12       <c:when test="${ requestScope.height > 170 }">
    13         <h2>还可以</h2>
    14       </c:when>
    15       <c:otherwise>
    16           <c:choose>
    17             <c:when test="${requestScope.height > 160}">
    18               <h3>大于 160</h3>
    19             </c:when>
    20             <c:when test="${requestScope.height > 150}">
    21               <h3>大于 150</h3>
    22             </c:when>
    23             <c:when test="${requestScope.height > 140}">
    24               <h3>大于 140</h3>
    25             </c:when>
    26             <c:otherwise>
    27               其他小于 140
    28             </c:otherwise>
    29           </c:choose>
    30       </c:otherwise>
    31     </c:choose>

      6、<c:foreach /> 标签

        作用:遍历输出使用

        常用属性:

    ① var: 设置遍历出对象的名称;

    ② items:指定要遍历的集合对象,可以是数组、字符串和各种集合;

    ③ begin:指定遍历的开始位置,int 类型;

    ④ end:指定遍历的结束位置,int 类型;

    ⑤ step:迭代的步长,int 类型;

    ⑥ varStatus:指定保存迭代状态的对象的名字,该变量引用是一个 LoopTagStatus 类型的对象,通过该对象可以获取一些遍历的状态

          a、count:表示遍历的个数

          b、index:表示当前的索引数,从1开始

          c、first:表示当前数据是否是第一个

          d、last:表示当前数据是否是最后一个

        (1)遍历 1 到 10,输出

            示例:

    1 <%--1.遍历 1 到 10, 输出
    2      begin 属性设置开始的索引
    3      end 属性设置结束的索引
    4      var 属性表示循环的变量(也是当前正在遍历到的数据)
    5     相当于 for(int i = 0;i<10;i++)
    6 --%>
    7  <c:forEach begin="1" end="10" var="i">
    8     第${i}个 <br/>
    9  </c:forEach>

        (2)遍历 Object 数组

           示例:

     1 <%-- 2.遍历 Object 数组
     2         for (Object item: arr)
     3         items 表示遍历的数据源(遍历的集合)
     4         var 表示当前遍历到的数据
     5 --%>
     6 <%
     7       request.setAttribute("arr", new String[]{"123","456","789"});
     8  %>
     9 <c:forEach items="${ requestScope.arr }" var="item">
    10       ${ item } <br>
    11 </c:forEach>

        (3)遍历 Map 集合

            示例:

     1 <%
     2       Map<String,Object> map = new HashMap<String, Object>();
     3       map.put("key1", "value1");
     4       map.put("key2", "value2");
     5       map.put("key3", "value3");
     6       // for ( Map.Entry<String,Object> entry : map.entrySet()) {}
     7 
     8       request.setAttribute("map", map);
     9 %>
    10 <c:forEach items="${ requestScope.map }" var="entry">
    11       
    12       <h1>${entry.key} = ${entry.value}</h1>
    13       
    14 </c:forEach>

        (4)遍历 List 集合—list中存放 Student类,有属性:编号,用户名,密码,年龄,电话信息

           示例:

     1  // Student 类   
     2  public class Student {
     3       private Integer id;
     4       private String username;
     5       private String password;
     6       private Integer age;
     7       private String phone;
     8  }
     9     <%
    10       List<Student> studentList = new ArrayList<Student>();
    11       for (int i = 1; i <= 10; i++) {
    12         studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i));
    13       }
    14       request.setAttribute("stus", studentList);     <%-- 放入 request 域中--%>
    15     %>
    16     <table>
    17         <tr>
    18           <th>编号</th>
    19           <th>用户名</th>
    20           <th>密码</th>
    21           <th>年龄</th>
    22           <th>电话</th>
    23           <th>操作</th>
    24         </tr>
    25         <%--
    26             items 表示遍历的集合
    27             var 表示遍历到的数据
    28             begin 表示遍历的开始索引值
    29             end 表示结束的索引值
    30             step 属性表示遍历的步长值
    31             varStatus 属性表示当前遍历到的数据的状态
    32             forint i = 1; i < 10; i+=2)()
    33           --%>
    34       <c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
    35           <tr>
    36             <td>${ stu.id }</td>
    37             <td>${ stu.username }</td>
    38             <td>${ stu.password }</td>
    39             <td>${ stu.age }</td>
    40             <td>${ stu.phone }</td>
    41             <td>${ status.current }</td>
    42             <td>${ status.index }</td>
    43             <td>${ status.count }</td>
    44             <td>${ status.first}</td>
    45             <td>${ status.last }</td>
    46             <td>${ status.begin }</td>
    47             <td>${ status.end }</td>
    48             <td>${ status.step }</td>
    49           </tr>
    50       </c:forEach>
    51     </table>

          其实 Varstatus 是一个内部类,它又实现了 LoopTagStatus 接口,当使用这些属性时,会调用对应的方法。

          其中 index 表示容器中元素的索引,从0开始,count 表示遍历的个数,是从1开始的。

          

       7、<c:redirect>

         作用:主要用于将请求重定向到另一个资源地址

        语法:

    格式:  <c:redirect url=" "></c:redirect>

    属性说明:① url 表示指定重定向的目标地址,这里指定绝对路径会自动加上项目名称

        Demo:

      

  • 相关阅读:
    网站初学笔记1
    如何在新项目中使用曾经创建的用户控件
    C#用户控件的一些尝试
    MVC的含义
    Chrome浏览器面板基础了解
    VScode快捷键
    Windows中的键盘快捷方式
    node.js状态码
    JavaScript小结
    Truncate a string
  • 原文地址:https://www.cnblogs.com/niujifei/p/15117169.html
Copyright © 2011-2022 走看看