带有上下文路径的 Spring Boot 静态内容

svg*_*svg 6 java spring spring-boot

使用上下文路径时,我在 Spring Boot 中遇到静态内容问题。即:我希望将我的应用程序部署到localhost:8080/{appname}/

当我在没有上下文路径的情况下运行应用程序时,一切正常,Spring Boot 找到并运行我的.html文件(来自资源/模板/,我正在使用Thymeleaf)和 JS 文件(来自资源/静态/js/)但是当我添加上下文路径时与:

server.context-path=/{appname}
Run Code Online (Sandbox Code Playgroud)

或者

server.servlet-path=/{appname}
Run Code Online (Sandbox Code Playgroud)

那么.html页面仍然显示由JS文件产生404错误

我曾试图改变spring.resources.static-locationsapplication.properties并覆盖addResourceHandlers()在我的方法,MvcConfig.class但既不似乎工作

我使用一个MvcConfig类是因为我需要定义 aCookieLocaleResolver和 aMessageSource但这就是我在MvcConfig. 我不使用@EnableWebMvc,只有@SpringBootApplication带有@ComponentScan.

任何帮助,将不胜感激 :)

rhi*_*nds 5

根据您的评论:

HTML 引用了没有应用程序上下文的 JS

问题不在于 Spring 服务 js,而是页面没有正确创建资源的 URL。

Thymeleaf 提供了一种机制来自动支持这一点,只需使用th前缀标记 src 属性即可。

请参阅第 2 节“上下文相关 URL”:www.thymeleaf.org/doc/articles/standardurlsyntax.html