Thymeleaf + Spring-Boot-为什么我不能访问静态资源? [英] Thymeleaf + Spring-Boot - why can't I access static resources?

查看:368
本文介绍了Thymeleaf + Spring-Boot-为什么我不能访问静态资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目树如下:

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

I can access the templates now, but can't load static resources such as CSS, images, and JS.

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

I have a common.html fragment where I declare all my static resources, for instance:

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

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

A header fragment where I include common.html like so:

<!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文件中,我具有以下条目:

Also, on my application.properties file, I have these entries:

#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消息.

But I keep getting the same No mapping found message.

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'

我在这里没看到什么?

推荐答案

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

I solved this problem with the following code:

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

这篇关于Thymeleaf + Spring-Boot-为什么我不能访问静态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆