简体   繁体   中英

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:

<%@ 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?

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

<%@ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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