简体   繁体   English

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

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

I want to add a custom Jsp tag to Spring boot.我想将自定义 Jsp 标签添加到 Spring 引导。 But I can't figure out project structure/URI for the taglib definition但我无法弄清楚 taglib 定义的项目结构/URI

I tried searching for solutions, but they are too... deprecated.我尝试寻找解决方案,但它们也……已被弃用。 Using web.xml stuff, or really old folder structure which spring boot doesn't support by default.使用 web.xml 东西,或者 spring 引导默认不支持的非常旧的文件夹结构。

While trying to load jsp page o get this error org.apache.jasper.JasperException: Unable to find taglib [ex] for URI: [/customTags.tld]尝试加载 jsp 页面时出现此错误org.apache.jasper.JasperException: Unable to find taglib [ex] for URI: [/customTags.tld]

This is my current folder structure这是我当前的文件夹结构

├───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 - I am inheriting from parent module, so version are missing 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自定义.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索引.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>

Any help is appreciated, Thank you任何帮助表示赞赏,谢谢

So project structure was good.所以项目结构很好。 The reason why it couldnt find taglib is that uri is not a filepath its a domain (you can make it up)它找不到 taglib 的原因是 uri 不是文件路径而是域(你可以弥补)

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

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

and in jsp call the custom tag using <%@ taglib prefix="ex" uri="http:/whatever.jstldomainexample"%> index.jsp并在 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