IntelliJ Spring Boot项目无法通过Thymeleaf找到我的CSS文件 [英] IntelliJ Spring Boot project can't find my CSS files with Thymeleaf

查看:101
本文介绍了IntelliJ Spring Boot项目无法通过Thymeleaf找到我的CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Spring Boot and my problem is that I have Spring Boot project and I am intending to view my HTML pages with Thymeleaf but Spring can't resolve my JavaScript and CSS files.

我的IDE的完整图片:

这些是我的Thymeleaf配置

Those are my Thymeleaf configurations

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页面标题:

This is my HTML page head:

<!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>

推荐答案

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

You should use relative path to your JavaScript and CSS files:

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

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

or you can use th:href Thymeleaf's tag as well:

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

这篇关于IntelliJ Spring Boot项目无法通过Thymeleaf找到我的CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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