La Carbonell
La Carbonell

Reputation: 2056

Thymeleaf : create URL with parameters

I have this function in a Thymeleaf template to create the URL

/company/delete/id

with this function

function confirmDelete (id) {
        var r = confirm("Are you sure to delete the item #" + id + " ?");
        if (r == true) {
            var link = /*[[@{/company/delete/}]]*/ + id;
            window.location.href = link;
        } 
}

but the id is ignored and the URL generated is /company/delete/ without the id

Upvotes: 0

Views: 924

Answers (2)

Metroids
Metroids

Reputation: 20477

When you comment out your Thymeleaf/javascript like that, Thymeleaf is attempting to make JavaScript natural templates work. It deletes everything after the end of the comment because it thinks you are trying to make the template work both using Thymeleaf to process, and just viewing it in your browser without processing.

You should just move where you are appending the id.

function confirmDelete (id) {
  var r = confirm("Are you sure to delete the item #" + id + " ?");
  if (r == true) {
    var link = /*[[@{/company/delete/}]]*/ "";
    window.location.href = link + id;
  } 
}

Upvotes: 1

N4zroth
N4zroth

Reputation: 1366

For JavaScript, you have to add th:inline="javascript" to your enclosing tag. I had to add this to make it work, too:

<script th:inline="javascript">
                /*<![CDATA[*/
                url: /*[('"' + @{/api/project/search/findProjectsForUser} + '"')]*/ "/api/project/search/findProjectsForUser",
                /*]]>*/
</script>

Upvotes: 0

Related Questions