1

I am practising thymeleaf template engine for the first time. I have followed the tutorial and so on but I have no idea where I am going wrong.

My controller:

public String mainPage(Model model){
    model.addAttribute("data", "Hello Thymeleaf");  
    return "main";
}

and my html is as follows:

<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
   <h1>th:text="${data}"</h1>
</body>
</html>

When I hit localhost it displays th:text="${data}" instead of Hello Thymeleaf

<h1>"${data}"</h1>

doesn't work either. View resolver config must be correct as it resolves main to main.html. I am using spring4 SpringTemplateEngine and spring4 thymeleaf view resolver.

thanks in advance

3 Answers 3

10

You have to use th:text

 <h1 th:text="${data}"></h1>

or if you don't want to use the th:text attribute, then you have to use th:inline="text" and make the thymeleaf render the context inside the tag. But make sure you put the variable inside [[ and ]]

 <h1 th:inline="text">[[${data}]]</h1>
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't realised you and chrylis answered before my edit. thanks I had the th:text= on the wrong side of >
3

Thymeleaf isn't Velocity or Freemarker and doesn't replace expressions blindly. You need the expression in an appropriate tag attribute, such as

<h1 data-th-text="${data}" />

Comments

-4

remove quotes to "${data}" and just use ${data}. I also agree with @Faraj Farook

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.