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

gtl*_*wig 3 css spring static-resource thymeleaf spring-boot

我的项目树如下所示:

在此输入图像描述

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

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

<link rel="stylesheet" th:href="@{/css/app.css}"/>
Run Code Online (Sandbox Code Playgroud)

我包含的标题片段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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

另外,在我的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
Run Code Online (Sandbox Code Playgroud)

但我不断收到同样的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'
Run Code Online (Sandbox Code Playgroud)

我在这里没有看到什么?

小智 5

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

spring.resources.static-locations=classpath:templates/
Run Code Online (Sandbox Code Playgroud)