通过Spring Boot在JSP中使用自定义标签文件 [英] Using custom Tag files in JSP with Spring Boot

查看:694
本文介绍了通过Spring Boot在JSP中使用自定义标签文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot项目,正在尝试在JSP文件中进行以下调用:

I have a Spring Boot project and I'm trying to make the following call in a JSP file:

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

<tagz:utils tabs="true"/>

标签文件夹位于-

\ src \ main \ resources \ WEB-INF \ tags

\src\main\resources\WEB-INF\tags

JSP文件文件夹位于-

The JSP files folder is in -

\ src \ main \ resources \ META-INF \ resources \ WEB-INF \ jsp

\src\main\resources\META-INF\resources\WEB-INF\jsp

我还定义了application.properties文件以包括:

I also defined the application.properties file to include:

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

如果我尝试将标签文件夹放在除Intellij之外的任何其他类路径中,则会显示错误,它无法在编辑器中识别该调用.

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.

如果我删除taglib调用,将正确显示JSP页面.

The JSP page is presented properly if I remove the taglib call.

我的pom.xml当然具有以下依赖性:

My pom.xml is of course has these dependencies:

 <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>

我收到以下错误:

Whitelabel错误页面

Whitelabel Error Page

此应用程序没有针对/error的显式映射,因此您看到了 作为后备.

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jun 25 16:12:47 IDT 2017发生意外错误 (类型=内部服务器错误,状态= 500). /WEB-INF/jsp/main.jsp (行:[11],列:[4])标签库中未定义标签[utils] 以前缀[tagz]

Sun Jun 25 16:12:47 IDT 2017 There was an unexpected error (type=Internal Server Error, status=500). /WEB-INF/jsp/main.jsp (line: [11], column: [4]) No tag [utils] defined in tag library imported with prefix [tagz]

我认为这与Spring Boot中静态文件的配置有关,但是我尝试添加 spring.resources.static-

I think It has to do with configuration of static files in Spring Boot but I tried to add 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" %>

有帮助吗?

推荐答案

因此,经过大量的跟踪和错误,我现在将所有JSP文件放在路径中:

So after a lot of trail and error I now put all my JSP files inside of path:

webapp/WEB-INF/jsp

标记以下文件:

webapp/WEB-INF/tags

其中的Tlds文件:

webapp/WEB-INF/tld

当您在JSP中调用标记/tld文件时,需要将它们引用到相对路径,例如:

When you call the tag/tld files you inside of the JSPs you need to refer them to relative path e.g:

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

您还需要在项目结构中定义Facet.如果没有它,则定义意味着您需要通过在项目中添加"Web框架"来生成它.它将生成web.xml,您需要将其放置在webapp/WEB-INF下,并手动在Facets项目中对其进行编辑.

You will also need to define a Facet in project structure. If you don't have it define that means that you need to generate it by adding "web framework" to your project. It will generate web.xml and you need to put it under webapp/WEB-INF and edit it in project Facets manaully.

希望这会对看到此帖子的人有所帮助.

Hope this will help anyone who sees this post.

这篇关于通过Spring Boot在JSP中使用自定义标签文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆