说说在 Spring Boot 中如何集成 JSP 标签库

773 阅读2分钟

在之前的文章中已经说过如何在 Spring Boot 中集成 JSP 视图技术。现在说说如何集成 JSP 标签库。 JSP 标签库可以是标准标签库,也可以是自定义标签库。

1 标准标签库

JSTL 是 JSP 标准标签库1,全称是 Java server pages standarded tag library。引入后,我们就可以使用 JSP 标准标签库,比如 fmt 格式化标签库。

1.1 引入依赖包

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>${jstl-version}</version>
</dependency>

版本号是:

<jstl-version>1.2</jstl-version>

1.2 使用标准标签库

在 jsp 中引入标准标签库。

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

然后使用它们:

<c:set var="balance" value="120000.2309"/>
<p>格式化数字 (1): <fmt:formatNumber value="${balance}"
                                type="currency"/></p>

示例是先用 core 标签库设置一个金额,然后使用 fmt 标签库对其进行货币格式化。

2 自定义标签库

2.1 新建标签库定义文件

在 webapp/WEB-INF 下新建 tld 文件夹。然后按照标签库定义的语法,新建一个标签库文件,放置在 tld 文件夹下。

这里新建了一个名为 custom.tld 的标签库,实际上来源于 fmt 的标签库,只不过改变了一些基础配置信息。

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">

    <description>custom library</description>
    <display-name>deniro core</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>custom</short-name>
    <uri>http://deniro.net/jsp/jstl/custom</uri>

    <tag>
        <description>
            Formats a numeric value as a number, currency, or percentage
        </description>
        <name>formatNumber</name>
        <tag-class>com.fsti.ctcc.ivr10000.tag.FormatNumberTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <description>
                Numeric value to be formatted.
            </description>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Specifies whether the value is to be
                formatted as number, currency, or
                percentage.
            </description>
            <name>type</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Custom formatting pattern.
            </description>
            <name>pattern</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                ISO 4217 currency code. Applied only
                when formatting currencies (i.e. if type is
                equal to "currency"); ignored otherwise.
            </description>
            <name>currencyCode</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Currency symbol. Applied only when
                formatting currencies (i.e. if type is equal
                to "currency"); ignored otherwise.
            </description>
            <name>currencySymbol</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Specifies whether the formatted output
                will contain any grouping separators.
            </description>
            <name>groupingUsed</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Maximum number of digits in the integer
                portion of the formatted output.
            </description>
            <name>maxIntegerDigits</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Minimum number of digits in the integer
                portion of the formatted output.
            </description>
            <name>minIntegerDigits</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Maximum number of digits in the
                fractional portion of the formatted output.
            </description>
            <name>maxFractionDigits</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Minimum number of digits in the
                fractional portion of the formatted output.
            </description>
            <name>minFractionDigits</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Name of the exported scoped variable
                which stores the formatted result as a
                String.
            </description>
            <name>var</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <description>
                Scope of var.
            </description>
            <name>scope</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
</taglib>

2.2 配置自定义标签库

在 web.xml 文件中配置自定义标签库:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Archetype Created Web Application</display-name>

    <jsp-config>
        <taglib>
            <taglib-uri>http://deniro.net/jsp/jstl/custom</taglib-uri>
            <taglib-location>/WEB-INF/tld/custom.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

taglib-uri 定义 uri,这个 uri 会在 jsp 中进行引用。

taglib-location 定义标签库的相对路径。

2.3 使用自定义标签库

先定义引用:

<%@ taglib prefix="custom"
           uri="http://deniro.net/jsp/jstl/custom" %>

然后使用它:

<p>格式化数字 (2): <custom:formatNumber value="${balance}"
                                type="currency"/></p>

3 设置为外部容器启动

因为 Spring Boot 不支持在可执行的 jar 中使用 jsp2,所以我们必须将其设置为 war 格式,然后在外部启动容器。

首先在 pom.xml 中将 packaging 配置为 war:

<packaging>war</packaging>

然后在外部启动容器。IDEA 中是点击上端的容器列表按钮,然后点击 Edit Configurations:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nFydLMZP-1630635373090)(C:\Users\86135\AppData\Roaming\Typora\typora-user-images\image-20210902170247533.png)]

在打开的面板中,新建一个 Tomcat Server,接着在 Deployment 页签中绑定工程 war 包:

在这里插入图片描述

运行结果:

在这里插入图片描述


Footnotes

  1. baike.baidu.com/item/JSTL/1…

  2. docs.spring.io/spring-boot…