157

I am using Thymeleaf as template engine. How I pass a variable from Spring model to JavaScript variable?

Spring-side:

@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
    model.addAttribute("message", "hello");
    return "index";
}

Client-side:

<script>
    ....
    var m = ${message}; // not working
    alert(m);
    ...
</script>
Vy Do
  • 46,709
  • 59
  • 215
  • 313
Matteo
  • 1,893
  • 2
  • 14
  • 13

10 Answers10

264

According to the official documentation:

<script th:inline="javascript">
/*<![CDATA[*/

    var message = /*[[${message}]]*/ 'default';
    console.log(message);

/*]]>*/
</script>
vdenotaris
  • 13,297
  • 26
  • 81
  • 132
30

Thymeleaf 3 now:

  • Display a constant:

    <script th:inline="javascript">
    var MY_URL = /*[[${T(com.xyz.constants.Fruits).cheery}]]*/ "";
    </script>
    
  • Display a variable:

    var message = [[${message}]];
    
  • Or in a comment to have a valid JavaScript code when you open your template file in a static manner (without executing it at a server).

    Thymeleaf calls this: JavaScript natural templates

    var message = /*[[${message}]]*/ "";
    

    Thymeleaf will ignore everything we have written after the comment and before the semicolon.

More info: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#javascript-inlining

redochka
  • 12,345
  • 14
  • 66
  • 79
  • thanks you! wanna gave you a beer i was looking for this syntax var MY_URL = /*[[${T(com.xyz.constants.Fruits).cheery}]]*/ ""; – Aung Aung Nov 24 '18 at 10:48
22
var message =/*[[${message}]]*/ 'defaultanyvalue';
Natix
  • 14,017
  • 7
  • 54
  • 69
user5518390
  • 221
  • 2
  • 2
  • 9
    Notice it should be NO space between /* */ and the contained [[ ]]. – jyu Feb 15 '16 at 19:49
  • 1
    It's worth noting that the `defaultanyvalue` will only be used when running the page statically, i.e. outside a web container. If ran inside a container and the variable `message` hasn't been declared the resulting source code will be `var message = null;` – Felipe Leão May 10 '17 at 17:51
  • 6
    Also important to add `th:inline="javascript"` to the script tag. – redent84 Aug 06 '18 at 18:12
14

According to the documentation there are several ways to do the inlining.
The right way you must choose based on the situation.

1) Simply put the variable from server to javascript :

<script th:inline="javascript">
/*<![CDATA[*/

var message = [[${message}]];
alert(message);

/*]]>*/
</script>

2) Combine javascript variables with server side variables, e.g. you need to create link for requesting inside the javascript:

<script th:inline="javascript">
        /*<![CDATA[*/
        function sampleGetByJquery(v) {
            /*[+
            var url = [[@{/my/get/url(var1=${#httpServletRequest.getParameter('var1')})}]] 
                      + "&var2="+v;
             +]*/
            $("#myPanel").load(url, function() {});
        }
        /*]]>*/
        </script>

The one situation I can't resolve - then I need to pass javascript variable inside the Java method calling inside the template (it's impossible I guess).

sanluck
  • 1,544
  • 1
  • 12
  • 22
14

MAKE sure you have thymleaf on page already

//Use this in java
@Controller
@RequestMapping("/showingTymleafTextInJavaScript")
public String thankYou(Model model){
 model.addAttribute("showTextFromJavaController","dummy text");
 return "showingTymleafTextInJavaScript";
}


//thymleaf page  javascript page
<script>
var showtext = "[[${showTextFromJavaController}]]";
console.log(showtext);
</script>
Nitish Kanade
  • 201
  • 2
  • 4
8

I've seen this kind of thing work in the wild:

<input type="button"  th:onclick="'javascript:getContactId(\'' + ${contact.id} + '\');'" />
Noumenon
  • 5,099
  • 4
  • 53
  • 73
7

If you use Thymeleaf 3:

<script th:inline="javascript">
    var username = [[${session.user.name}]];
</script>
Togrul Sadigov
  • 231
  • 4
  • 10
  • 1
    This should get more votes, now that Thymeleaf 3 is pretty much the norm, yes? The manual link should also be updated. https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#text-inlining ...I just shipped a float[] with audio data to a web page for the first time. Can play it with Web Audio API! This question and these answer were a big help in locating the Javascript inlining used. – Phil Freihofner May 08 '21 at 05:51
3

Assuming request attribute named "message":

request.setAttribute("message", "this is my message");

To read it in the html page using Thymeleaf template:

<script>
  var message = "[[${message}]]";
  alert(message);
</script>
C J
  • 298
  • 4
  • 13
3

If you need to display your variable unescaped, use this format:

<script th:inline="javascript">
/*<![CDATA[*/

    var message = /*[(${message})]*/ 'default';

/*]]>*/
</script>

Note the [( brackets which wrap the variable.

michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
3

Another way to do it is to create a dynamic javascript returned by a java controller like it is written here in the thymeleaf forum: http://forum.thymeleaf.org/Can-I-use-th-inline-for-a-separate-javascript-file-td4025766.html

One way to handle this is to create a dynamic javascript file with the URLs embedded in it. Here are the steps (if you are using Spring MVC)

@RequestMapping(path = {"/dynamic.js"}, method = RequestMethod.GET, produces = "application/javascript")
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public String dynamicJS(HttpServletRequest request) {

        return "Your javascript code....";

}


  
Paolo Biavati
  • 609
  • 10
  • 19