Thymeleaf 3 upgrade - JS URLs no longer have quotes

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Thymeleaf 3 upgrade - JS URLs no longer have quotes

cooldman224
This post was updated on .
Hello,

I recently upgraded from Thymeleaf 2.1.5 to 3.0.1 with Spring Boot 1.4.0. Everything is smooth so far except with
<script th:inline="javascript">
 where my URL from
[[@{/}]]
 no longer has quotes around them when generated.

See my example below:

index.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
...
</head>
<body>
...
<script th:inline="javascript" th:include="~{index.js :: script}"></script>
</body>
</html>

index.js.html
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<script th:fragment="script">
/*<![CDATA[*/
$(function() {
   $.ajax({
     url: [[@{/my/url}]],
     success: function(data) {
       ...
     }
   });
});
/*]]>*/
</script>
</body>
</html>

index.html Becomes:

<html>
<head>
...
</head>
<body>
...
<script>
/*<![CDATA[*/
$(function() {
   $.ajax({
     url: /my/url, // should be '/my/url' !
     success: function(data) {
       ...
     }
   });
});
/*]]>*/
</script>
</body>
</html>

Is this a bug or am I missing something that somehow worked on 2.1.x? Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf 3 upgrade - JS URLs no longer have quotes

danielfernandez
Administrator
Your code has th:fragment="javascript" instead of th:inline="javascript". So there is no JavaScript inlining applied. Which means your URLs are simply text-inlined, not turned into JavaScript literals…
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf 3 upgrade - JS URLs no longer have quotes

cooldman224
Oops! You are very correct! Sorry about that. My example meant to say
th:inline="javascript"
 I've updated my OP with more code for context. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf 3 upgrade - JS URLs no longer have quotes

cooldman224
Found the solution for anyone interested. So not only URLS but all other JS literals don't have quotes.

I needed to do put th:inline="javascript" also in the fragment.

So my index.js.html becomes
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<script th:inline="javascript" th:fragment="script">
/*<![CDATA[*/
$(function() {
   $.ajax({
     url: [[@{/my/url}]],
     success: function(data) {
       ...
     }
   });
});
/*]]>*/
</script>
</body>
</html>

And the original th:inline="javascript" in index.html stays from the fragment declaration. Not work if the th:inline="javascript" is deleted.

<html xmlns:th="http://www.thymeleaf.org">
<head>
...
</head>
<body>
...
<script th:inline="javascript" th:include="~{index.js :: script}"></script>
</body>
</html>

Fairly interesting that is worked in Thymeleaf 2.1.x but not 3