Wilson Campusano
Wilson Campusano

Reputation: 616

How to inject @Value into thymeleaf inline javascript

I have this inline javascript using thymeleaf

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

    var contextPath = [[@Value("#{servletContext.contextPath}")]];

    /*]]>*/
</script>

When my document y parsed and send to the client this is what is printed

<script>
    /*<![CDATA[*/

    var contextPath = </script>

I have tried this

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

    var contextPath = [[${servletContext.contextPath}]];

    /*]]>*/
</script>

but I'm getting the same result.

servletContext is the reference to the javax.servlet.ServletContext interface

Upvotes: 0

Views: 515

Answers (1)

davioooh
davioooh

Reputation: 24706

What you need is explained in Thymeleaf documentation.

Try with this:

var contextPath = /*[[${#ctx.servletContext.contextPath}]]*/ null;

Upvotes: 1

Related Questions