IVR Avenger
IVR Avenger

Reputation: 15494

String is null on Thymeleaf page after adding to model

My controller:

@Controller
public class IndexController {

    @RequestMapping(value = "/index")
    public String index(Model model) {
        model.addAttribute("message", "Hello World!");
        return "index";
    }
}

My page:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
   <head>
      <meta charset = "ISO-8859-1" />
      <link href = "css/styles.css" rel = "stylesheet"/>
      <title>Spring Boot Application</title>
   </head>
   <body>
      <h4>Spring boot.</h4>
      <p th:utext="${message}"></p>
   </body>
</html>

Upon loading and rendering, this HTML appears; how do I get the message to appear?

<!DOCTYPE html>
<html>
   <head>
      <meta charset = "ISO-8859-1" />
      <link href = "css/styles.css" rel = "stylesheet"/>
      <title>Spring Boot Application</title>
   </head>
   <body>
      <h4>Spring boot.</h4>
      <p></p>
   </body>
</html>

Upvotes: 0

Views: 101

Answers (2)

IVR Avenger
IVR Avenger

Reputation: 15494

Method in IndexController is never being called, because this:

@RequestMapping(value = "/index")

Should be this:

@RequestMapping(value = "/")

Upvotes: 0

Khalil Kabara
Khalil Kabara

Reputation: 168

Ensure you have imported the thymeleaf dependency into your project. Use th:text="${message}"

Upvotes: 1

Related Questions