2

我的项目树如下所示:

在此处输入图像描述

我现在可以访问模板,但无法加载 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

4 回答 4

5

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

spring.resources.static-locations=classpath:templates/
于 2020-05-28T02:35:37.220 回答
3

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

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

您可以提供逗号分隔的值,即取自文档

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
于 2017-09-20T14:31:33.207 回答
2

我正在使用 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/");
    }

}

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


于 2020-12-12T20:36:00.783 回答
0

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

<link rel="stylesheet" th:href="@{/css/app.css}"
href="../../../css/app.css" />
于 2017-09-20T14:33:51.220 回答