Search code examples
springthymeleaf

I can't display image in HTML through Thymeleaf in Spring


I do an example from Spring in Action 5. I try to display the homepage with an image. When I start localhost:8080 I see the homepage, but don't see image on it.

I enter HTML from the book:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Taco Cloud</title>
</head>

<body>
    <h1>Welcome to...</h1>
    <img th:src="@{/images/TacoCloud.png}"/>
</body>
</html>

And I add an image in my project in Eclipse: project explorer

Add HomeController class:

package tacos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
    @GetMapping("/")
    public String home() {
        return "home";
    }
}

On display, I get next: enter image description here


Solution

  • I found why it doesn't work.

    When I was adding an image in Eclipse, I clicked right button mouse on src/main/resources/static/images then New-File-Link to file in the file system... I don't move an image in my project catalog. I only created to link on my image in the filesystem.

    So I added an image. It is not right...