spring-boot - 静态资源与 Spring Boot 中的 @PathParam 冲突 thymleaf localhost:8080/script.js 与 localhost:8080/{message} 冲突
问题描述
主要课程-
@SpringBootApplication
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
控制器类
@Controller
public class GreetingController {
@GetMapping("/{message}")
public String greeting(@PathVariable(name="message", required=false) String message, Model model) {
model.addAttribute("message", message);
return "greeting";
}
}
html文件
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="/script.js"></script>
</head>
<body>
<p th:text="'Hello, ' + ${message} + '!'" />
</body>
</html>
脚本文件
console.log("hi");
索引.html
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>
此外,此项目参考来自https://spring.io/guides/gs/serving-web-content/并进行了以下修改
- 在
GreetingController
而不是通过query param
接受参数path param
- 将脚本文件添加到
/resources/static
文件夹 - 在
greeting.html
添加的一行中包含script file
解决方案
您最好发布完整的 home.html 文件和完整的 Controller 类。或尝试 @PathVariable 而不是 @PathParam 。由于 @PathParam 只能与 REST 一起使用,而 @PathVariable 在 Spring 中使用,因此它可以在 MVC 和 REST 中使用。
推荐阅读
- python-3.x - Microsoft Graph Python SDK 缓存?
- flutter - 解析块映射时需要一个键。颤振错误
- python - 我想在 python 中使用 selenium 登录 Morningstar
- javascript - 如何使用 JavaScript split() 函数同时拆分空格和符号
- dask - 手动设置 dask config 日志记录参数
- github - github - 为动作定义变量
- java - 如何使用 Java FXGL 创建三角形
- javascript - 如何使用 javascript 删除 CSS 类属性?
- node.js - Heroku 部署失败,出现“错误代码=H10”:Vue.js + NodeJS 应用
- java - 实例化方法的调度对象
失败的