Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 当我用Thymeleaf运行Spring Boot项目时,浏览器赢了';无法加载css文件,但href和th:href的路径正确_Html_Css_Spring_Thymeleaf - Fatal编程技术网

Html 当我用Thymeleaf运行Spring Boot项目时,浏览器赢了';无法加载css文件,但href和th:href的路径正确

Html 当我用Thymeleaf运行Spring Boot项目时,浏览器赢了';无法加载css文件,但href和th:href的路径正确,html,css,spring,thymeleaf,Html,Css,Spring,Thymeleaf,所以我读了很多关于我的问题的文章和帖子,比如: 但是没有一个答案能真正帮助我,所以我要问我自己的问题 目前的情况是: 我有一个使用thymeleaf的springboot项目,在resources/templates下有一个html文件,在resources/templates/css下也有一个css 结构如下: src 主要 资源 静止的 css “我的css文件” 模板 “我的html文件” 这是我的html文件: <!DOCTYPE htm

所以我读了很多关于我的问题的文章和帖子,比如:

但是没有一个答案能真正帮助我,所以我要问我自己的问题

目前的情况是:

我有一个使用thymeleaf的springboot项目,在resources/templates下有一个html文件,在resources/templates/css下也有一个css

结构如下:

  • src
    • 主要
      • 资源
        • 静止的
          • css
            • “我的css文件”
        • 模板
          • “我的html文件”
这是我的html文件:

<!DOCTYPE html>
<html lang="de" xmlns:th="http://www.thymeleaf.org">

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Welcome to Mreza Mladih</title>
        <link rel="stylesheet" type="text/css" href="../static/css/styles.css" th:href="@{../static/css/styles.css}">
        <link href="https://fonts.googleapis.com/css2?family=Lato&family=Raleway&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300&display=swap" rel="stylesheet">
    </head>
有趣的是,我的css实际上是正确的,而且可以工作! 我使用intellij,IDE有一个html文件的预览功能,它实际上可以工作

如果有人能帮上忙并感谢你,那就太好了


来自德国的问候语

Spring Boot为应用程序根目录下的
src/main/resources/static
中的任何内容提供服务

因此,使用Thymeleaf获取CSS的URL应该是
/CSS/styles.CSS

按如下方式编辑HTML文件:

<link rel="stylesheet" type="text/css" href="../static/css/styles.css" th:href="@{/css/styles.css}">
您还可以使用:

http.authorizeRequests()
    .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
         .permitAll()
    .antMatchers("/registration**",
                 "/img/**")
        .permitAll()
PathRequest.toStaticResources().atCommonLocations()
包括
/css/*
/js/*
/images/*
/webjars/*
favicon.ico

.antMatchers(
                    "/registration**",
                    "/js/**",
                    "/css/**",
                    "/img/**").permitAll()
http.authorizeRequests()
    .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
         .permitAll()
    .antMatchers("/registration**",
                 "/img/**")
        .permitAll()