IntelliJ Spring Boot项目无法使用Thymeleaf找到我的CSS文件

时间:2016-11-06 17:34:48

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

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

我的IDE的全貌: Full picture of my 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>

2 个答案:

答案 0 :(得分:3)

您应该使用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"/>

答案 1 :(得分:0)

我发现Spring会自动搜索:/resources/
因此,我使用find all删除了所有预目录。

因此,在我的情况下,例如:/static/css/style.css/templates/index.html

成为:/css/style.css/index.html

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