Spring 引导:创建自定义 Jsp 标记 - 无法找到 taglib
Spring Boot: Create Custom Jsp Tag - Unable to find taglib
我想将自定义 Jsp 标签添加到 Spring 引导。但是我无法找出 taglib 定义
的项目 structure/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
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>
感谢任何帮助,
谢谢
所以项目结构很好。它找不到 taglib 的原因是 uri 不是文件路径它是域(你可以弥补)
在 tld 文件中添加此 <uri>http://whatever.jstldomainexample</uri>
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 中调用自定义标签使用
<%@ 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>
我想将自定义 Jsp 标签添加到 Spring 引导。但是我无法找出 taglib 定义
的项目 structure/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
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>
感谢任何帮助, 谢谢
所以项目结构很好。它找不到 taglib 的原因是 uri 不是文件路径它是域(你可以弥补)
在 tld 文件中添加此 <uri>http://whatever.jstldomainexample</uri>
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 中调用自定义标签使用
<%@ 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>