JSTL 标准标签库

原文:https://www.studytonight.com/jsp/jstl-in-jsp.php

JSP 标准标签库(JSTL)是现成标签的标准库。JSTL 包含几个标签,通过提供一些现成的、已经实现的通用功能,可以从 JSP 页面中删除脚本代码。

JSTL 分为 5 组:

  1. JSTL 核心: JSTL 核心提供 if、forEach、import、out 等几个核心标签,支持一些基本的脚本任务。JSP 页面中包含 JSTL 核心标签的网址为→

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
  2. JSTL 格式化: JSTL 格式化库提供标签来格式化国际化网站的文本、日期和数字。在 JSP 页面中包含 JSTL 格式标签的网址是→

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
  3. JSTL sql: JSTL sql 库支持关系数据库连接和标签,对 SQL 数据库执行插入、删除、更新、选择等操作。JSP 页面中包含 JSTL SQL 标签的网址是→

    <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
    
  4. JSTL XML: JSTL XML 库提供对 XML 处理的支持。它提供流量控制、转换功能等。JSP 页面中包含 JSTL XML 标签的网址是→

    <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
    
  5. JSTL 函数: JSTL 函数库支持字符串操作。JSP 页面中包含 JSTL 函数标签的网址为→

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

JSP JSTL 核心图书馆

JSTL 核心库包含几个标签,可以用来消除基本的脚本开销,例如 JSP 页面中的for循环、if...else条件等。让我们研究一下 JSTL 核心图书馆的一些重要标签。

  • JSTL if 标记:if 标记是用于计算条件表达式的条件标记。当一个主体被提供了if标记时,只有当表达式为真时,才计算该主体。例如:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
      <head>
        <title>Tag Example</title>
      </head>
      <body>
        <c:if test="${param.name == 'studytonight'}">
          <p>Welcome to ${param.name} </p>
        </c:if>
      </body>
    </html>
    

  • JSTL out tag: The out tag is used to evaluate an expression and write the result to JspWriter. For Example :

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
      <head>
        <title>Tag Example</title>
      </head>
      <body>
        <c:out value="${param.name}" default="StudyTonight" />
      </body>
    </html>
    

    value属性指定要写入 JspWriter 的表达式。default属性指定表达式计算为空时要写入的值。


  • JSTL forEach tag: This tag provides a mechanism for iteration within a JSP page. JSTL forEach tag works similarly to enhanced for loop of Java Technology. You can use this tag to iterate over an existing collection of items. For Example :

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
      <head>
        <title>Tag Example</title>
      </head>
      <body>
        <c:forEach var="message" items="${errorMsgs}" >
          <li>${message}</li>
        </c:forEach>
      </body>
    </html>
    

    这里,属性项的值是一个 EL 表达式,它是错误消息的集合。迭代中的每一项都将存储在一个名为 message 的变量中,该变量在 forEach 标记的主体中可用。


  • JSTL choose, when, otherwise tag: These are conditional tags used to implement conditional operations. If the test condition of the when tag evaluates to true, then the content within when tag is evaluated, otherwise the content within the otherwise tag is evaluated.

    我们也可以通过使用多个 when 标签来实现if-else-if构造。当标签是互斥的,这意味着评估为真的第一个当标签被评估,然后,控制退出choose块。如果当条件都不为真,则评估otherwise条件。例如

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Tag Example</title>
  </head>
  <body>
    <c:forEach var="tutorial" items="${MyTutorialMap}" begin="0" end="5" varStatus="status">
      <c:choose>
        <c:when test="${status.count %2 == 0 }">
          <p> Divisible by 2 : ${tutorial.key} </p>
          <br/>
        </c:when>
        <c:when test="${status.count %5 == 0 }">
          <p > Divisible by 5 : ${tutorial.key} </p>
            <br/>
        </c:when>
        <c:otherwise>
            <p> Neither divisible by 2 nor 5 : ${tutorial.key} </p><br/>
        </c:otherwise>
      </c:choose>
    </c:forEach>
  </body>
</html>

  • JSTL 导入标签: < c:import>标签用于在请求时将提供的网址中的内容动态添加到当前页面。< c:import>网址属性中使用的网址资源可以来自网络容器之外。例如:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
      <head>
        <title>Tag Example</title>
      </head>
      <body>
        <c:import url="http://www.example.com/hello.html">>
        <c:param name="showproducts" value="true"/>
        </c:import>
      </body>
    </html>
    

  • JSTL url 标签:JSTL url 标签用于将 url 存储在变量中,必要时还会执行 URL 重写。例如
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Tag Example</title>
  </head>
  <body>
    <a href='<c:url value="/home.jsp"/>' > Go Home </a>
  </body>
</html>

  • JSTL 集合标记:JSTL 集合标记用于存储指定范围内的变量或更新 JavaBean 实例的属性。下面是设置学生 bean 的名称属性的示例:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
      <head>
        <title>Tag Example</title>
      </head>
      <body>
        <c:set target="student" property="name" value="${param.name}" />
      </body>
    </html>
    

  • JSTL catch 标记:JSTL catch 标记用于处理异常,不会将页面转发到错误页面。例如:

     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
      <head>
        <title>Tag Example</title>
      </head>
      <body>
        <c:catch>
          <% int a = 0;
             int b = 10;
             int c = b/a;
          %>
         </c:catch>
      </body>
    </html>