163

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>

10 Answers 10

269

According to the official documentation:

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

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

/*]]>*/
</script>
Sign up to request clarification or add additional context in comments.

8 Comments

Doesn't work... javascript error uncaught syntax error
Works fine, also possible to read from messages.properties: var msg = [[#{msg}]];
@szxnyc if you forget the /*<![CDATA[*/ macro you will get that.
Also take attention to <script th:inline="javascript">
@MichałStochmal you can load inline javascript on top of external javascript and use same variables(defined in inline javascript) in external javascript.
|
34

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

1 Comment

thanks you! wanna gave you a beer i was looking for this syntax var MY_URL = /*[[${T(com.xyz.constants.Fruits).cheery}]]*/ "";
23
var message =/*[[${message}]]*/ 'defaultanyvalue';

3 Comments

Notice it should be NO space between /* */ and the contained [[ ]].
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;
Also important to add th:inline="javascript" to the script tag.
15

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>

Comments

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).

Comments

10

If you use Thymeleaf 3:

<script th:inline="javascript">
    var username = [[${session.user.name}]];
</script>

2 Comments

This should get more votes, now that Thymeleaf 3 is pretty much the norm, yes? The manual link should also be updated. thymeleaf.org/doc/tutorials/3.0/… ...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.
VsCode has problems with this syntax, but the code works fine. If you want vscode to stop complaining, use this alterative syntax: var username = "[(${session.user.name})]".
8

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

<input type="button"  th:onclick="'javascript:getContactId(\'' + ${contact.id} + '\');'" />

Comments

4

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>

1 Comment

Have XSS vulnerability!
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.

Comments

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....";

}


  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.