Search code examples
javahtmlspring-bootintellij-ideathymeleaf

Thymeleaf does not show data in html page


I’m just studying Thymeleaf and I don’t understand why I can’t show data inside my html code. My Thymeleaf controller is as follows:

@Controller
public class UserController {
    @GetMapping("/index")
    public String getMessage(Model model) {
        model.addAttribute("welcome", "Hello!");
        return "index";
    }
}

While in my index.html I have something like this:

<div>
   <p th:text="${welcome}"></p>
</div>

The dependencies in the pom.xml file are as follows:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

Finally the structure of my "resources" folder is as follows:

resources
|
|--- css
|--- img
|--- js
|--- login.html
|---templates
    |
    |--- index.html

If it helps I’m working with SpringBoot 3.2.0 on a "macos Sonoma 14.2" system and I created my project by adding the "Spring Bot Dev-tool" and the "Spring Web". Finally, I can specify that making the Get request via Postman works perfectly, it returns the html page with the added message. This does not happen when I start my page as an intellij. Thanks to all who will help me!


Solution

  • The code is fine that is also why postman shows you the correct templated page content.

    When you render the HTML in the IntelliJ "browser" it does not render it on the server and the Thymeleaf engine does not set data from the model, it's just a preview.

    Just query the controller via a usual browser.