我在春季启动应用程序的静态文件夹下加载文件时遇到问题。
问题是 RequestMapping 深度超过 2 喜欢@RequestMapping("spring/xyz")
单一深度效果很好,@RequestMapping("spring")
但 2 深度以“弹簧”为前缀,它连接 localhost:8080/spring/“静态文件夹”
我在这里找到了一半的解决方案
我的文件夹结构是:
static/css/some.css
static/templates/velocity.vm
案例1:效果很好
java:
@RequestMapping("spring")
html:
<link rel="stylesheet" href="css/some.css">
案例2:效果很好
java:
@RequestMapping("spring/xyz")
html:
<link rel="stylesheet" href="../css/some.css">
案例3:不工作
java:
@RequestMapping("spring/xyz/123")
html:
<link rel="stylesheet" href="../css/some.css">
它被称为'http//localhost/spring/xyz/css/some.css'
案例3:效果很好
java:
@RequestMapping("spring/xyz/123")
html:
<link rel="stylesheet" href="../../css/some.css">
案例4:效果很好
java:
@RequestMapping("123")
html:
<link rel="stylesheet" href="../../css/some.css">
有用!!即使我使用../../
相对路径。我不知道为什么会这样。
实际上我不太了解 Spring Boot API,我考虑使用 ViewResoler 加载其他静态资源。
我想知道这个加载路径机制以及如何通过 RequestMapping url 路径链接来调用“http//localhost/spring/xyz/css/some.css”
我感谢任何答案~!
我在spring.io上从“metalhead”和“Brian Clozel”中提到了同样的问题