【问题标题】:IntelliJ Spring Boot project can't find my CSS files with ThymeleafIntelliJ Spring Boot 项目无法使用 Thymeleaf 找到我的 CSS 文件
【发布时间】:2017-03-20 00:57:49
【问题描述】:

我是 Spring Boot 新手,我的问题是我有 Spring Boot 项目,我打算用 Thymeleaf 查看我的 HTML 页面,但 Spring 无法解析我的 JavaScript 和 CSS 文件。

我的 IDE 的全貌:

这些是我的 Thymeleaf 配置

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=true

这是我的 HTML 页面标题:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
       >
<head>
    <title>404 Page</title>
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <!--[if lt IE 9]>
    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
    <![endif]-->
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <meta charset="UTF-8" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1"/>
    <meta name="viewport" content="width=device-width"/>
    <!-- Css Files Start -->
    <link href="/src/main/resources/static/css/style.css" rel="stylesheet" type="text/css" media="screen"/><!-- All css -->
    <link href="/src/main/resources/static/css/bs.css" rel="stylesheet" type="text/css" media="screen"/><!-- Bootstrap Css -->
    <link rel="stylesheet" type="text/css" href="/src/main/resources/static/css/main-slider.css" media="screen"/><!-- Main Slider Css -->
    <!--[if lte IE 10]>
    <link rel="stylesheet" type="text/css" href="/static/css/customIE.css" media="screen"/><![endif]-->
    <link href="/src/main/resources/static/css/font-awesome.css" rel="stylesheet" type="text/css" media="screen"/><!-- Font Awesome Css -->
    <link href="/src/main/resources/static/css/font-awesome-ie7.css" rel="stylesheet" type="text/css" media="screen"/><!-- Font Awesome iE7 Css -->
    <noscript>
        <link rel="stylesheet" type="text/css" href="/static/css/noJS.css"/>
    </noscript>
    <!-- Css Files End -->
</head>

【问题讨论】:

  • 如果您检查控制台视图,您会遇到哪些错误?它通常有助于包括错误。在这种情况下,我猜路径可能不正确 - 但如果没有你得到的实际输出,这很难验证。

标签: spring spring-mvc intellij-idea spring-boot thymeleaf


【解决方案1】:

您应该使用 JavaScript 和 CSS 文件的相对路径:

<link href="../../static/css/style.css" rel="stylesheet" type="text/css" media="screen"/>

或者你也可以使用th:href Thymeleaf 的标签:

<link th:href="@{css/style.css}" href="../../static/css/style.css"
      rel="stylesheet" type="text/css" media="screen"/>

【讨论】:

    【解决方案2】:

    我发现 Spring 会自动搜索:/resources/
    所以我所做的是使用 find all 删除所有预目录。

    所以,在我的例子中,例如:/static/css/style.css & /templates/index.html

    变成:/css/style.css & /index.html

    现在的问题仍然是 Spring boot 如何在不定义文件夹的情况下知道要查看的文件夹:Spring 查看扩展名。当 Spring Boot 看到 .html 时,它会在名为 /templates 的文件夹中查找它。

    【讨论】:

      猜你喜欢
      • 2018-08-31
      • 2018-03-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2017-08-14
      • 2015-06-16
      • 2016-12-07
      • 2017-06-14
      相关资源
      最近更新 更多