I have a Spring Boot project, and I need to render a part of a script element like the following. (Assume that getMessage function is defined somewhere else.)
Expected output:
<script type="text/javascript">
/*<![CDATA[*/
var message = 'Hello, John' + getMessage();
alert(message);
/*]]>*/
</script>
where John is a Spring model attribute.
Is it possible to make the whole 'Hello, John' + getMessage() in a single Thymeleaf expression? Or, is assigning Hello, John and getMessage() to separate JavaScript variables then concatenating them the only way? I tried something like the following but it seems inlined JavaScript expression always gets quoted.
<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
var message = /*[[|'Hello, ${name}' + getMessage()|]]*/ 'Hello, John';
alert(message);
/*]]>*/
</script>
Actual output:
<script type="text/javascript">
/*<![CDATA[*/
var message = "'Hello, John' + getMessage()";
alert(message);
/*]]>*/
</script>
By the way, I'm using Thymeleaf 2.1.