Reputation: 4065
I'm facing issues with images in my Spring Boot + Thymeleaf application.
After reading a lot of fixes I'm able to show images in some of my app's pages but in other pages the images doesn't show.
I think that the number of paths in the request made is involved. It seems that requests to /myaction
render pages showing images while requests to /myaction/other
render pages not showing images.
In the fomer the successful request to obtain the images is:
http://localhost:8080/myapp/images/logo.png
In the latter the failed request to obtain the images is:
http://localhost:8080/myapp/myaction/images/logo.png
I append my configuration:
In my implementation of 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 class:
@Controller
@RequestMapping(path="/myaction")
public class PagosController {
@GetMapping(path="")
public String show(Model model) {
//...
}
@GetMapping(path="/other")
public String show2(Model model) {
//...
}
}
In my html template I load the image this way:
<img th:src="@{images/logo.png}" />
logo.png
The file logo.png
is located in src/main/resources/static/images
I have no clue why this is happening. Any idea on why images are getting requested at http://localhost:8080/myapp/myaction/images/logo.png
? Thanks in advance.
Upvotes: 0
Views: 269
Reputation: 6936
According to your configuration, the images are available from the root /.
So you should be able to use <img th:src="@{/images/logo.png}" />
in any page.
Upvotes: 1