Search code examples
spring-bootthymeleaf

Why Thymeleaf for href?


I am deploying a template that comprises Spring Boot, Thymeleaf and Bootstrap.

Why does the template need the Thymeleaf component? Normally, Thymeleaf is used to populate the index.html-datatable by receiving data from the controller. But in this case, Thymeleaf is only used in front of the „href“, e.g. like:

[...] th:rel=“stylesheet“ th:href=“@{assets/datatable/datatables.css}"/>

Why do I need Thymeleaf in this case? Isn't it possible to create such references in normal HTML just without the th-prefixes? Is it really necessary to use Thymeleaf here? If yes: Why?


Solution

  • Yes, it's possible to create links without th:href. In this case, doesn't look like there is any reason to use it (since it's a relative link).

    That being said, if the link was context relative (starting with a slash @{/assets/datatable/datatables.css}), using th:href would let you deploy the application to different contexts without having to change the link. If you didn't use a th:href you would have to change the code to deploy to a differnt context. E.g.

    href="/my-application/assets/datatable/datatables.css"
    

    wouldn't work if you decided to change the application context to something like /my-online-portal/.

    This: th:href="@{/assets/datatable/datatables.css}" would work in both cases and produce /my-application/assets/datatable/datatables.css or /my-online-portal/assets/datatable/datatables.css depending on the context it's deployed to.