htmlspringeclipsespring-bootthymeleaf

How to use Thymeleaf in html with variable included inside of a h1 tag?


So I just started working with Thymeleaf and Im not sure exactly how to use this with the html tags. I searched for documentation but I couldn't find anything about this specific issue. It seems so simple but i'm having trouble displaying a h1 html tag that has a variable from my controller in it.

So I Have my basic controller which looks like this:

@SpringBootApplication
@Controller
public class HomeController {
    @RequestMapping("/")
    public String index(Model model, @RequestParam(value="name", required=false, defaultValue="Human") String name) {
        model.addAttribute("name", name);
        return "home/index.html";
    }

}

and I have my html that looks like this:

    <!DOCTYPE html>
<html lang="en">
<head th:replace="fragments::header"></head>
<body>
<h1 th:text="Hello + ${name}"> </h1>
<p>Welcome to Spring Boot!</p>
</body>
</html>

when I load my page it looks like "Helloname" with no space between the Hello and the variable name. How can I fix this? And am I using this the correct way?


Solution

  • SOLVED

    <!DOCTYPE html>
    <html lang="en">
    <head th:replace="fragments::header"></head>
    <body>
      <h1 th:text="|Hello ${name}|"></h1>
      <p>Welcome to Spring Boot!</p>
    </body>
    </html>