CSS 文件未加载到不同的 url
我有两个端点,每个端点都返回“index.html”视图。
http://localhost:8080/ 这个 URL 显示了索引文件并且 CSS 也正常工作。 图片
http://localhost:8080/product/1 此 URL 仅显示索引文件,但 CSS 未加载。 图片
MainController.java
package com.example.temporary.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/product/1")
public String process() {
return "index";
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" th:href="@{css/index.css}">
<title>Document</title>
</head>
<body>
<h1>Index Page</h1>
<a th:href="@{/product/1}">Click Me!</a>
</body>
</html>
index.css
* {
box-sizing: border-box;
}
body {
background-color: black;
color: white;
}
文件结构 图片
I have two endpoints each of them is returning the "index.html" view.
http://localhost:8080/
this URL shows the index file and CSS is working as well. imagehttp://localhost:8080/product/1
this URL only shows the index file but CSS is not loading. image
MainController.java
package com.example.temporary.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/product/1")
public String process() {
return "index";
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" th:href="@{css/index.css}">
<title>Document</title>
</head>
<body>
<h1>Index Page</h1>
<a th:href="@{/product/1}">Click Me!</a>
</body>
</html>
index.css
* {
box-sizing: border-box;
}
body {
background-color: black;
color: white;
}
File Structure
image
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在 html 中包含如下的 css
而不是 css/index.css 它应该是 /css/index.css
try to include css as below in html
instead of css/index.css it should be /css/index.css