2
I’m using Spring and Thymeleaf:
I have an image in the directory:
Resources/image/Telescope.png
When I open the page posts.html (home),located at the address below, the image is displayed.
Code to call the image in posts.html (home):
<img src="resources/imagem/telescope.png">
But when I click on a link from the home page to call a service in the Homecontroller:
@RequestMapping(value = "/categoria/{link}", method = RequestMethod.GET)
public ModelAndView postByCategoria(@PathVariable("link") String link, ModelMap model) {
List<Postagem> postagems = postagemService.findByCategoria(link);
model.addAttribute("postagens", postagems);
return new ModelAndView("posts.html", model);
}
The above method returns a view to the page posts.html again ,I can no longer load the image what is happening?
In inspecting it says it can not carry the image src that is mounted so by Spring:
http://localhost:8080/Search services/category/Resources/image/Telescope.png
The image really isn’t at this address! She is still here:
Resources/image/Telescope.png
I don’t understand why he created this concatenation for the image src!
In the configuration class I enabled everything on the Resources page to be loaded:
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
...
...
You passed a relative address, so he "agreed," if you put a
/
at the image address, the behavior will not be that– Denis Rudnei de Souza
What do you use in the View? html only?
– Denis Rudnei de Souza
If you use Thymeleaf you can do so:
<img th:src="@{/resources/imagem/telescope.png}">
– Denis Rudnei de Souza
@Denisrudneidesouza Thanks! That’s right, I forgot to mention I was wearing Hymeleaf!!! It’s been a while since I worked with Thymeleaf!! It was shot and fall!!
– Pena Pintada