Thymeleaf Javascript Variable

Thymeleaf

@GetMapping()
    public String getIndex(Model model){
        model.addAttribute("name","dogukan");
        return "index";
    }

This code will assign name variable to javascript user variable. When we open .html file in browser without application server it will assign UserName to user, so javascript code will not break.

<p th:text="${name}"/>

 <script th:inline="javascript">

     /*<![CDATA[*/

     var user = /*[[${name}]]*/ 'UserName';

     /*]]>*/
    console.log(user);
 </script>

Similar Posts:
  1. Thmeleaf Standard URL Syntax
  2. Thmeleaf Message (i18n) expression
  3. Thymeleaf th:switch th:case

Tags: spring, spring framework, spring mvc, Thymeleaf, thymeleaf javascript, thymeleaf javascript variable, thymeleaf syntax