Spring Boot 无法从用户文件夹加载 css [英] Spring boot cannot load css from user folder

查看:64
本文介绍了Spring Boot 无法从用户文件夹加载 css的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发 spring boot 和 thymeleaf 的 Web 应用程序.我设法通过从文件夹resources/static"调用资源 css 和 js 在 web-front 中进行设置.

I am currently developing a web application of the spring boot and thymeleaf. I managed to set up in the web-front by calling the resources css and js from the folder "resources/static".

但是,对于resources/template/app/index.html"文件夹中的用户空间,我无法在resources/static/app_css/bootstrap.css"中调用我的css.

However, for user space in the "resources/template/app/index.html" folder, I can not call my css in "resources/static/app_css/bootstrap.css".

这是我的文件树:

我的 index.html 来自 resources/templates/app/

My index.html from resources/templates/app/

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
    <head th:include="app_fragments/header :: header">
        <p>Header</p>
    </head>
    <body>
   <!-- Indicates a successful or positive action -->
<button type="button" class="btn btn-success">Success</button>
    </body>
    </html>

来自 resources/templates/app_fragments/的我的标题片段:

My header fragment from resources/templates/app_fragments/:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:fragment="header">
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
    <meta name="description" content=""/>
    <meta name="author" content=""/>
    <link rel="shortcut icon" th:href="@{../resources/app_fonts/app_images/favicon.png}" href="../resources/static/app_fonts/app_images/favicon.png"  type="image/png"/>
    <title>Toto</title>
<!-- Plugins CSS -->
<link rel="stylesheet" type="text/css" media="all" th:href="@{../resources/static/app_css/bootstrap.min.css}" href="../resources/static/app_css/bootstrap.min.css" />
</head>
</html>

当我进入用户空间时,我无法从

when I go into the user space, I can't load my Bootstrap file from http://localhost:8081/app/index.html

如何解决?

提前致谢.

推荐答案

您应该阅读如何使用 Spring Boot 提供静态内容".

You should read on "How to serve static content with Spring Boot".

我已经在这里回答了这个问题:

I already answered this question here:

Spring Boot CSS尝试一切后显示空白/未加载

长话短说,首先将您的静态内容移动到正确的文件夹,这里引用了 spring 文档:

To make a long story short, first move your static content to the proper folder, here's a quote from spring docs:

默认情况下,Spring Boot 将从目录中提供静态内容在中称为/static(或/public 或/resources 或/META-INF/resources)类路径或从 ServletContext 的根目录

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext

之后,您需要进行一些配置以破坏浏览器缓存.

after that, you'll need to make some configuration to bust your browser cache.

尝试先清理浏览器缓存,在谷歌浏览器中,您可以转到设置,然后清除浏览数据.

try first clean your browser cache, in google chrome you go to settings then clear browsing data.

其次,将这些行添加到您的 application.properties 文件中以破坏缓存:

Secondly, add these lines to your application.properties file in order to bust the cache:

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

或改为添加:

spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/**
spring.resources.chain.strategy.fixed.version=v12

这篇关于Spring Boot 无法从用户文件夹加载 css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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