问题描述
我想给 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>
感谢任何帮助, 谢谢
解决方法
所以项目结构很好。找不到taglib的原因是uri不是文件路径而是域(可以自己编)
在 tld 文件中添加此 executeScript
custom.tld
<uri>http://whatever.jstldomainexample</uri>
并在 jsp 中使用调用自定义标记
<?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>
index.jsp
<%@ taglib prefix="ex" uri="http:/whatever.jstldomainexample"%>