Springboot开发中的一些小坑——CSS失效问题

Springboot版本1.5.17
结合thymeleaf,在项目中引用CSS文件的问题

	    <parent>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-parent</artifactId>
	        <version>1.5.17.RELEASE</version>
	        <relativePath/> <!-- lookup parent from repository -->
	    </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
<!DOCTYPE html >
<html  lang="zh-CN" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8" />
    <title>0.0</title>
    <link  rel="stylesheet" type="text/css"  href="../static/css/index.css">
</head>

首先配置的CSS引用是这样,href后面跟上从static文件后的完整路径,打开静态网页就是有css效果了
demo.CSS载入
没有加载成功是这样的
在这里插入图片描述
但是问题来了,还有种说法是这样加:

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

而在静态网页中,你看到的,始终是带上了CSS样式的结果,但Springboot项目运行起来后,你会发现CSS加载失效了,所以如果是Springboot项目,一定要加上后面的路径th:href=""。

猜你喜欢

转载自blog.csdn.net/hu421160052/article/details/85336519