繁体   English   中英

Spring 引导:创建自定义 Jsp 标记 - 找不到标记库

[英]Spring Boot: Create Custom Jsp Tag - Unable to find taglib

我想将自定义 Jsp 标签添加到 Spring 引导。 但我无法弄清楚 taglib 定义的项目结构/URI

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

尝试加载 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

应用程序属性

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>

你好标签

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());
        }
    }
}

自定义.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>

索引.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>

任何帮助表示赞赏,谢谢

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

在 tld 文件中添加这个<uri>http://whatever.jstldomainexample</uri>

自定义.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 中使用<%@ taglib prefix="ex" uri="http:/whatever.jstldomainexample"%> 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM