JSP Standard Tag Library (JSTL) is a collection of JSP tags, which encapsulates many commonly used functions and calls them in the form of xml tags.
It is mainly divided into 5 categories of tags:
- core tag;
- formatting tags;
- SQL label;
- XML action tags;
- String manipulation labels.
MAVEN installation
Add the pom.xml
following to:
<!-- jstl start -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- jstl end -->
JSTL main tags
core tag
The core tag library mainly contains tags that perform basic operations.
It is referenced as follows:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
It includes the following tags:
Label | describe |
---|---|
<c:out> |
Used to display data in JSP like <%= … > |
<c:set> |
for saving data |
<c:remove> |
to delete data |
<c:catch> |
Used to handle exception conditions that generate errors and store error information |
<c:if> |
Same as if we use in general programs |
<c:choose> |
It is only used as <c:when> the <c:otherwise> parent tag of and |
<c:when> |
<c:choose> The subtag of , which is used to determine whether the condition is true |
<c:otherwise> |
<c:choose> The sub-tag, after the tag , is executed <c:when> when the tag is judged to be false<c:when> |
<c:import> |
Retrieve an absolute or relative URL and expose its content to the page |
<c:forEach> |
Basic iteration tag that accepts multiple collection types |
<c:forTokens> |
Delimit the content according to the specified delimiter and iterate the output |
<c:param> |
Used to pass parameters to pages that contain or redirect |
<c:redirect> |
redirect to a new URL |
<c:url> |
Use optional query parameters to create a URL |
formatting tags
The format tag library mainly contains tags for formatting data.
It is referenced as follows:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
It includes the following tags:
Label | describe |
---|---|
<fmt:formatNumber> |
Formats a number with the specified format or precision |
<fmt:parseNumber> |
Parse a string representing a number, currency or percentage |
<fmt:formatDate> |
Format date and time using the specified style or pattern |
<fmt:parseDate> |
Parse a string representing a date or time |
<fmt:bundle> |
bind resources |
<fmt:setLocale> |
designated area |
<fmt:setBundle> |
bind resources |
<fmt:timeZone> |
Specify time zone |
<fmt:setTimeZone> |
Specify time zone |
<fmt:message> |
Display resource profile information |
<fmt:requestEncoding> |
set request character encoding |
SQL tags
The SQL tag library mainly contains tags that interact with the database.
It is referenced as follows:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
It includes the following tags:
Label | describe |
---|---|
<sql:setDataSource> |
Specify the data source |
<sql:query> |
run SQL query |
<sql:update> |
run SQL update statement |
<sql:param> |
Set SQL the parameter in the statement to the specified value |
<sql:dateParam> |
Sets the date parameter in the SQL statement to the specified java.util.Date object value |
<sql:transaction> |
在共享数据库连接中提供嵌套的数据库行为元素,将所有语句以一个事务的形式来运行 |
XML 操作标签
XML 操作标签库主要包含了用于对.xml
文件进行操作的标签。
它的引用方式如下:
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
它包括了以下标签:
标签 | 描述 |
---|---|
<x:out> |
与<%= ... > ,类似,不过只用于XPath 表达式 |
<x:parse> |
解析XML 数据 |
<x:set> |
设置XPath 表达式 |
<x:if> |
判断XPath 表达式,若为真,则执行本体中的内容,否则跳过本体 |
<x:forEach> |
迭代XML 文档中的节点 |
<x:choose> |
<x:when> 和<x:otherwise> 的父标签 |
<x:when> |
<x:choose> 的子标签,用来进行条件判断 |
<x:otherwise> |
<x:choose> 的子标签,当<x:when> 判断为false 时被执行 |
<x:transform> |
将XSL 转换应用在XML 文档中 |
<x:param> |
与<x:transform> 共同使用,用于设置XSL 样式表 |
格式化标签
字符串操作标签库主要包含了对字符串做一些常用操作的标签。
它的引用方式如下:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
它包括了以下标签:
标签 | 描述 |
---|---|
fn:contains() |
测试输入的字符串是否包含指定的子串 |
fn:containsIgnoreCase() |
测试输入的字符串是否包含指定的子串,大小写不敏感 |
fn:endsWith() |
测试输入的字符串是否以指定的后缀结尾 |
fn:escapeXml() |
跳过可以作为XML 标记的字符 |
fn:indexOf() |
返回指定字符串在输入字符串中出现的位置 |
fn:join() |
将数组中的元素合成一个字符串然后输出 |
fn:length() |
返回字符串长度 |
fn:replace() |
将输入字符串中指定的位置替换为指定的字符串然后返回 |
fn:split() |
将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回 |
fn:startsWith() |
测试输入字符串是否以指定的前缀开始 |
fn:substring() |
返回字符串的子集 |
fn:substringAfter() |
返回字符串在指定子串之后的子集 |
fn:substringBefore() |
返回字符串在指定子串之前的子集 |
fn:toLowerCase() |
将字符串中的字符转为小写 |
fn:toUpperCase() |
将字符串中的字符转为大写 |
fn:trim() |
移除首位的空白符 |
自定义标签
创建一个标签类
自定义标签类需要继承SimpleTagSupport
类并重写的doTag()
方法:
package com.vingyun;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagTest extends SimpleTagSupport {
// 自定义标签属性
private String custom;
public String getCustom() {
return custom;
}
public void setCustom(String custom) {
this.custom = custom;
}
@Override
public void doTag() throws JspException, IOException {
// TODO 自动生成的方法存根
JspWriter out = getJspContext().getOut();
out.println("Hello " + custom + ", This is TagTest!!");
}
}
创建.tld文件
.tld
文件作为自定义标签的说明文件,我们需要在里面对标签进行配置:
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Tag Test TLD</short-name>
<tag>
<!-- 调用时的名称 -->
<name>tagTest</name>
<!-- 标签类的完全限定名 -->
<tag-class>com.vingyun.TagTest</tag-class>
<!-- 标签体可接受文本、EL和JSP动作。 -->
<body-content>scriptless</body-content>
<attribute>
<!-- 定义属性的名称,每个标签的是属性名称必须是唯一的 -->
<name>custom</name>
<!-- 指定属性是否是必须的或者可选的,如果设置为false为可选 -->
<required>false</required>
<!-- 声明在运行表达式时,标签属性是否有效 -->
<rtexprvalue>true</rtexprvalue>
<!-- 定义该属性的Java类类型 , 默认指定为 String -->
<type>String</type>
<!-- 描述信息 -->
<description>名称输入</description>
<!-- 该属性值是否需要被视为一个 JspFragment -->
<fragment>false</fragment>
</attribute>
</tag>
</taglib>
在这个例子中,我把它取名为tagTest.tld
。
自定义标签的使用
首先需要对自定义标签进行声明:
<%@ taglib prefix="t" uri="WEB-INF/tld/tagTest.tld"%>
然后就可以调用了:
<t:tagTest custom="Ving"></t:tagTest>
输出结果: