Using custom Tag files in JSP with Spring Boot

天大地大妈咪最大 提交于 2019-12-07 18:42:26

问题


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

The tags folder is in -

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

The JSP files folder is in -

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

I also defined the application.properties file to include:

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.

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

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>

I get the following error:

Whitelabel Error Page

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

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]

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

Any help?


回答1:


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

webapp/WEB-INF/jsp

Tags files inside of:

webapp/WEB-INF/tags

And Tlds files inside of:

webapp/WEB-INF/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" %>

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.



来源:https://stackoverflow.com/questions/44746757/using-custom-tag-files-in-jsp-with-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!