简体   繁体   English

通过Spring Boot在JSP中使用自定义标签文件

[英]Using custom Tag files in JSP with Spring Boot

I have a Spring Boot project and I'm trying to make the following call in a JSP file: 我有一个Spring Boot项目,正在尝试在JSP文件中进行以下调用:

<%@ taglib prefix="tagz" tagdir="/WEB-INF/tags" %>

<tagz:utils tabs="true"/>

The tags folder is in - 标签文件夹位于-

\\src\\main\\resources\\WEB-INF\\tags \\ SRC \\主\\资源\\ WEB-INF \\标签

The JSP files folder is in - JSP文件文件夹位于-

\\src\\main\\resources\\META-INF\\resources\\WEB-INF\\jsp \\ SRC \\主\\资源\\ META-INF \\资源\\ WEB-INF \\ JSP

I also defined the application.properties file to include: 我还定义了application.properties文件以包括:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

If I try to put the tags folder in any other classpath than Intellij is showing an error that It cannot identify the call in the editor. 如果我尝试将标签文件夹放在除Intellij之外的任何其他类路径中,则会显示错误,该错误无法在编辑器中识别该调用。

The JSP page is presented properly if I remove the taglib call. 如果删除taglib调用,将正确显示JSP页面。

My pom.xml is of course has these dependencies: 我的pom.xml当然具有以下依赖关系:

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.5.4.RELEASE</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>8.5.15</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0-alpha-1</version>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
</dependency>

I get the following error: 我收到以下错误:

Whitelabel Error Page 白标错误页面

This application has no explicit mapping for /error, so you are seeing this as a fallback. 此应用程序没有针对/ error的显式映射,因此您将其视为后备。

Sun Jun 25 16:12:47 IDT 2017 There was an unexpected error (type=Internal Server Error, status=500). Sun Jun 25 16:12:47 IDT 2017发生意外错误(类型=内部服务器错误,状态= 500)。 /WEB-INF/jsp/main.jsp (line: [11], column: [4]) No tag [utils] defined in tag library imported with prefix [tagz] /WEB-INF/jsp/main.jsp(行:[11],列:[4])在导入了前缀[tagz]的标签库中未定义标签[utils]

I think It has to do with configuration of static files in Spring Boot but I tried to add spring.resources.static- 我认为这与Spring Boot中静态文件的配置有关,但是我尝试添加spring.resources.static-

locations=classpath:/resources/static/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/WEB-INF/tags/,classpath:/WEB-INF/
spring.mvc.static-path-pattern=/resources/**

Nothing seems to work. 似乎没有任何作用。 I should mention that these taglibs are working properly! 我应该提到这些标签库工作正常!

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

Any help? 有什么帮助吗?

So after a lot of trail and error I now put all my JSP files inside of path: 因此,经过大量的反复试验,现在我将所有JSP文件放入路径中:

webapp/WEB-INF/jsp

Tags files inside of: 标记以下文件:

webapp/WEB-INF/tags

And Tlds files inside of: Tlds文件位于:

webapp/WEB-INF/tld

When you call the tag/tld files you inside of the JSPs you need to refer them to relative path eg: 当您在JSP内调用标记/ tld文件时,需要将它们引用到相对路径,例如:

<%@ taglib prefix="ui" tagdir="/WEB-INF/tags/ui" %>

You will also need to define a Facet in project structure. 您还需要在项目结构中定义Facet。 If you don't have it define that means that you need to generate it by adding "web framework" to your project. 如果没有定义,则意味着您需要通过在项目中添加“ Web框架”来生成它。 It will generate web.xml and you need to put it under webapp/WEB-INF and edit it in project Facets manaully. 它将生成web.xml,您需要将其放在webapp/WEB-INF并手动在项目Facets中对其进行编辑。

Hope this will help anyone who sees this post. 希望这对看到这篇文章的人有所帮助。

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

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