skip to Main Content

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!

2

Answers


  1. 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.

    Login or Signup to reply.
  2. @Controller
    public class UserController {
    @GetMapping("/index")
    public String getMessage() {
        Resource resource = new ClassPathResource(templates/index.html);
        String htmlContent = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
        htmlContent = htmlContent.replace("{{welcome}}", "Hello");
    
        return htmlContent;
        }
    } 
    Update Index.html as well
    <div>
       <p th:text="{{welcome}}"></p>
    </div>
    
    
    Replace with this code
    
        
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search