无法在Spring Boot中从自定义taglib加载静态资源

时间:2019-01-25 02:43:41

标签: spring spring-boot jsp-tags

我正在尝试使用Spring Boot,并且试图创建一个简单的自定义Tag,该Tag充当JSP的包装。尽管我在从该标记加载静态资源时遇到问题。

下面是我的目录结构:         

    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com....
    │   │   ├── resources
    │   │   │   ├── application.properties
    │   │   │   ├── static
    │   │   │   |    ├── js
    │   │   │   |    ├── css
    │   │   │   |    └── libraries
    │   │   │   |          ├── jquery
    │   │   │   |                └── jquery.min.js
    │   │   │   |          ├── bootstrap
    │   │   │   |          ├── ...
    │   │   └── webapp
    │   │          └── WEB-INF
    │   │                ├── tags
    │   │                     └── main.tag
    │   │                ├── tlds
    │   │                ├── views
    │   │                     └── productDetails.jsp
    │   └── test
    │       └── java
    │           └── com

只要加载页面,内容就可以了,因为我可以检查页面的HTML,但是静态资源加载失败,这给了我控制台错误:

jquery.min.js:1 Failed to load resource: the server responded with a status of 404 ()
localhost/:1 Refused to apply style from 'http://localhost:8080/product/libraries/bootstrap/bootstrap.min.css' because its MIME type ('application/json') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
jquery.min.js:1 Failed to load resource: the server responded with a status of 404 ()
localhost/:1 Refused to apply style from 'http://localhost:8080/product/libraries/bootstrap/bootstrap.min.css' because its MIME type ('application/json') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

一个有趣的事情是,当我使用CDN链接更改资源时,它可以正常工作。某种程度上说,这与Spring有关,但我不知道是什么。


main.tag如下:

<%@ tag description="Core Page Template" %>

<%@ attribute name="header" fragment="true" required="false" %>
<%@ attribute name="jsImports" fragment="true" required="false" %>

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="libraries/bootstrap/bootstrap.min.css">
    <!-- I've even tried all possible combinations of that link
         (e.g /libraries or /static/libraries or I moved the whole directory
         under /webapp or /webapp/WEB-INF/)
         but nothing seem to work. All the time I get 404 responses from the
         server -->
    <jsp:invoke fragment="header"/>
</head>
<body>

<jsp:doBody/>

<script src="libraries/jquery/jquery.min.js"></script>
</body>
</html>

我可以在像这样的JSP中使用哪个:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="tt" tagdir="/WEB-INF/tags" %>

<tt:main>
    <jsp:attribute name="header">
            <title>Products - List</title>
    </jsp:attribute>
    <jsp:body>
        Do body....
    </jsp:body>
</tt:main>

0 个答案:

没有答案