【问题标题】:Spring Boot: Create Custom Jsp Tag - Unable to find taglibSpring Boot:创建自定义 Jsp 标记 - 找不到 taglib
【发布时间】:2021-04-27 17:53:23
【问题描述】:

我想在 Spring boot 中添加一个自定义的 Jsp 标签。但我无法弄清楚 taglib 定义的项目结构/URI

我尝试寻找解决方案,但它们也……已弃用。使用 web.xml 的东西,或者 Spring Boot 默认不支持的非常旧的文件夹结构。

在尝试加载 jsp 页面时遇到此错误 org.apache.jasper.JasperException: Unable to find taglib [ex] for URI: [/customTags.tld]

这是我当前的文件夹结构

├───src
│   └───main
│       ├───java
│       │   └───com
│       │       └───training
│       │           └───bookstore
│       │               │   AppFrontend.java
│       │               │   Test.java
│       │               │
│       │               ├───controller
│       │               │       FrontendController.java
│       │               │       FrontendEndConfiguration.java
│       │               │
│       │               └───tag
│       │                       HelloTag.java
│       │
│       ├───resources
│       │   │   application.properties
│       │   │   custom.tld
│       │   │
│       │   └───META-INF
│       │       └───resources
│       │               custom.tld
│       │               index.jsp
│       │
│       └───webapp

application.properties

spring.mvc.view.suffix=.jsp

pom.xml - 我是从父模块继承的,所以缺少版本

    <artifactId>bookstore-frontend</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency> <!-- JSTL -->
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
        </dependency>


        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2.1-b03</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
            <scope>runtime</scope>
        </dependency>

        

        <dependency>
            <groupId>com.training</groupId>
            <artifactId>bookstore-shared</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.training</groupId>
            <artifactId>boostore-storage-client</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

HelloTag

public class HelloTag extends SimpleTagSupport {
    private String message;

    public void setMessage(String msg) {
        this.message = msg;
    }
    StringWriter sw = new StringWriter();
    public void doTag()
            throws JspException, IOException {
        if (message != null) {
            JspWriter out = getJspContext().getOut();
            out.println( message );
        } else {
            getJspBody().invoke(sw);
            getJspContext().getOut().println(sw.toString());
        }
    }
}

custom.tld

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD with Body</short-name>

<tag>
    <name>Hello</name>
    <tag-class>com.training.bookstore.tag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>message</name>
    </attribute>
</tag>
</taglib>

index.jsp

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

<%@ taglib prefix = "ex" uri = "/customTags.tld"%>
<html>
    <head>
    </head>
    <body>
        <strong>Hello from index page</strong>
        <ex:Hello message = "This is custom tag" />
    </body>
</html>

任何帮助表示赞赏, 谢谢

【问题讨论】:

    标签: java spring jsp taglib


    【解决方案1】:

    所以项目结构很好。找不到taglib的原因是uri不是文件路径而是域(你可以弥补)

    在 tld 文件中添加 &lt;uri&gt;http://whatever.jstldomainexample&lt;/uri&gt;

    custom.tld

    <?xml version="1.0" encoding="UTF-8"?>
    <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
            version="2.1">
        <tlib-version>1.0</tlib-version>
        <short-name>jstlContains</short-name> (could be anything)
        <uri>http://whatever.jstldomainexample</uri> (could vary as per choice)
    
    <tag>
        <name>Hello</name>
        <tag-class>com.training.bookstore.tag.HelloTag</tag-class>
        <body-content>scriptless</body-content>
    
        <attribute>
            <name>message</name>
        </attribute>
    
    </tag>
    </taglib>
    

    并在 jsp 中使用 &lt;%@ taglib prefix="ex" uri="http:/whatever.jstldomainexample"%&gt; index.jsp

    <%@ taglib prefix="ex" uri="http://bojan.jstldomain"%>
    <html>
        <head>
        </head>
        <body>
            <strong>Hello from index page</strong>
            <ex:Hello message = "This is custom tag" />
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多