我在编写一些代码时遇到了完全相同的问题Spring Boot + Spring MVC。使用 CDN 设置的 CSS 文件工作正常,而从我的 CSS 文件设置static/css
文件夹返回了 HTML 内容。
Example:
<!-- This first worked fine, loading all styles -->
<link th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"
href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet" media="screen" />
<!-- This second one returned the content of an HTML - Content Type text/html -->
<link rel="stylesheet" th:href="@{/css/login/style.css}" href="/css/login/style.css" />
过了一会儿我可以看到使用Chrome 开发工具内容返回给我的本地style.css
与我的 HTML 页面之一相同。
检查指向包含该内容的 HTML 文件的路由,我发现我使用了错误的属性@RequestMapping
配置。我有@RequestMapping(name="...")
, 代替@RequestMapping(path="...")
.
有问题的控制器
@RequestMapping(name = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("product", new Product());
return "product/edit";
}
控制器已更改
@RequestMapping(path = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("product", new Product());
return "product/edit";
}
变更房产后name
with path
一切开始正确加载。
奇怪的是,像这样的小错误竟然影响了我的整个程序。
希望它对面临同样问题的人有所帮助。