Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot + Thymeleaf + HTML replace meta tag content attribute with dynamic value

I have a requirement to populate the meta tag's content attribute in my html with a dynamic value. I am using Spring boot with thymeleaf templating engine. I tried looking up for a solution but all the solutions out there are either in bits or pieces or don't answer my questions directly. I dont want to use JQuery or any other javascript framework due to the nature of my project, hence posting this query.

Already tried various thymeleaf out of box functionalities

    @Value("${redirect.url}")
    String redirectUrl;

    @RequestMapping(value = "/")
    @CrossOrigin
    public String index( Model model) {
        model.addAttribute("url", redirectUrl);
        return "index";
    }
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="Refresh" content="0; url="/> <!-- Need to be able to populate dynamic value  by using thymeleaf-->
</head>
</html>

I except url tag to contain the actual URL passed from my controller

like image 281
whizKid Avatar asked Jul 02 '26 10:07

whizKid


1 Answers

Thymeleaf supports the th:content attribute natively. No need to go with a javascript hack. See the list of supported attributes.

<meta http-equiv="Refresh" th:content="|0; url=${url}|" />
like image 148
Metroids Avatar answered Jul 04 '26 01:07

Metroids