使用 Spring Boot + Spring MVC
编写一些代码时,我遇到了完全相同的问题。使用CDN设置的CSS文件运行良好,而我static/css文件夹中的CSS文件设置返回了HTML内容。
例:
href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet" media="screen" />
一段时间后,我发现使用 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,path所有内容均开始正确加载。
奇怪的是,这样的小错误如何影响了我的整个程序。
希望它对面临同样问题的人有所帮助。