1

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?

aquile hollins
  • 161
  • 1
  • 8
  • Use a text literal in single quotes, with a space in it: `

    `. That's the difference between a _text literal_ and a _literal token_. A good syntax summary can be found [here](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#standard-expression-syntax).
    – andrewJames Sep 01 '21 at 23:31
  • Does this answer your question? [Thymeleaf: Concatenation - Could not parse as expression](https://stackoverflow.com/questions/16119421/thymeleaf-concatenation-could-not-parse-as-expression). There are various different approaches/syntaxes shown in different answers - some may not be relevant to your case, but others should be. – andrewJames Sep 01 '21 at 23:35
  • 2
    If you're building strings like that, I recommend [literal substitutions](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#literal-substitutions). `

    `
    – Metroids Sep 01 '21 at 23:40
  • Thanks! i really like the literal substitution method. it seems a lot more easier. – aquile hollins Sep 01 '21 at 23:54
  • See also https://www.wimdeblauwe.com/blog/2021/01/11/string-concatenation-with-thymeleaf/ for the various options you have. – Wim Deblauwe Sep 02 '21 at 12:23

1 Answers1

-2

Adding a singe quote should work