Spring Boot在某些页面中显示图像,但在其他页面中不显示

时间:2018-08-30 15:56:15

标签: spring-boot thymeleaf

我在Spring Boot + Thymeleaf应用程序中遇到图像问题。

阅读了许多修复程序后,我可以在应用的某些页面上显示图像,但在其他页面上则不显示图像。

我认为所涉及的请求中涉及的路径数。看来,/myaction的请求呈现了显示图像的页面,而/myaction/other的请求则呈现了不显示图像的页面。

在fomer中,成功获取图像的请求是:

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上请求图像?预先感谢。

1 个答案:

答案 0 :(得分:1)

根据您的配置,可以从根 / 中获得图像。

因此,您应该可以在任何页面中使用<img th:src="@{/images/logo.png}" />

相关问题