【发布时间】:2019-02-05 15:24:12
【问题描述】:
我在 Spring Boot + Thymeleaf 应用程序中遇到图像问题。
阅读大量修复后,我能够在我的应用程序的某些页面中显示图像,但在其他页面中图像不显示。
我认为涉及到请求中的路径数。似乎对/myaction 的请求呈现显示图像的页面,而对/myaction/other 的请求呈现不显示图像的页面。
前面成功获取图片的请求是:
http://localhost:8080/myapp/images/logo.png
在后者中,获取图像的失败请求是:
http://localhost:8080/myapp/myaction/images/logo.png
我附加我的配置:
在我的 WebMvcConfigurerAdapter 实现中:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/"
};
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/**")
.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
控制器类:
@Controller
@RequestMapping(path="/myaction")
public class PagosController {
@GetMapping(path="")
public String show(Model model) {
//...
}
@GetMapping(path="/other")
public String show2(Model model) {
//...
}
}
在我的 html 模板中,我以这种方式加载图像:
<img th:src="@{images/logo.png}" />
logo.png
文件logo.png位于src/main/resources/static/images
我不知道为什么会这样。关于为什么在http://localhost:8080/myapp/myaction/images/logo.png 请求图像的任何想法?提前致谢。
【问题讨论】:
-
认为你需要使用
<img th:src="@{/images/logo.png}" />以/(root)开头,看看here -
@DirkDeyne 是的,你是对的(我很惭愧)。发表答案,我会接受。非常感谢。
标签: spring-boot thymeleaf