Thymeleaf + Spring-Boot - 为什么我不能访问静态资源?

时间:2017-09-20 14:16:43

标签: css spring spring-boot thymeleaf static-resource

我的项目树看起来像这样:

enter image description here

我现在可以访问模板,但无法加载CSS,图像和JS等静态资源。

我有一个common.html片段,我在其中声明了所有静态资源,例如:

<link rel="stylesheet" th:href="@{/css/app.css}"/>

我在其中包含common.html的标题片段:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head th:include="fragments/common :: commonFragment" lang="en"></head>
// page body
</html>

布局的default.html文件:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<head th:include="fragments/common :: commonFragment" lang="en">
    <meta charset="utf-8"/>
    <meta name="robots" content="noindex, nofollow"/>
    <title>Touch</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="content-dataType" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <link rel="shortcut icon" th:href="@{/static/images/favicon.ico}"  type="image/x-icon" />
</head>

<body>
<div th:id="defaultFragment" th:fragment="defaultFragment" class="container">
    <div id="header" th:replace="fragments/header :: headerFragment" />
        <div layout:fragment="content" />
</div>
</body>
</html>

此外,在我的application.properties文件中,我有以下条目:

#SPRING RESOURCE HANDLING
spring.resources.static-locations=classpath:/resources/

#THYMELEAF
spring.thymeleaf.cache = true
spring.thymeleaf.check-template = true
spring.thymeleaf.check-template-location = true
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.template-resolver-order=1

但我一直收到同样的No mapping found消息。

32190 [http-nio-8081-exec-2] WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/central/css/app.css] in DispatcherServlet with name 'dispatcherServlet'

我在这里看不到什么?

4 个答案:

答案 0 :(得分:1)

使用属性中的 spring.resources.static-locations 来定义静态资源位置(使其直接公开):

spring.resources.static-locations=classpath:/your/static/resources/here/like/central/css/

您可以提供以逗号分隔的值,即taken from the documentation

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.

答案 1 :(得分:1)

我用以下代码解决了这个问题:

spring.resources.static-locations=classpath:templates/

答案 2 :(得分:0)

尝试在链接中添加静态资源的映射

<link rel="stylesheet" th:href="@{/css/app.css}"
href="../../../css/app.css" />

答案 3 :(得分:0)

我正在使用Spring Boot 2.2,但没有得到任何静态内容。我发现了两种对我有用的解决方案:


选项1-停止使用@EnableWebMvc批注

此注释会禁用一些自动配置,包括自动从常用位置(如/src/main/resources/static)提供静态内容的部分。如果您真的不需要@EnableWebMvc,只需将其从您的@Configuration类中删除。


选项2-在您的WebMvcConfigurer带注释的类中实现@EnableWebMvc,并实现 addResourceHandlers()

执行以下操作:

@EnableWebMvc
@Configuration
public class SpringMVCConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/vendor/**").addResourceLocations("classpath:/static/vendor/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

}

请记住,您的代码现在负责管理所有静态资源路径。


相关问题