Search code examples
jqueryspring-mvcspring-bootspring-securitythymeleaf

get User ID in a Thymeleaf template


I have defined a URL in a Thymeleaf template to connect to a RESTful API where the logged userId is required, I've tried this but checking the source code of the template, the userId is not replaced

var ajaxUrl = /*[[@{/api/users/@{|~/${#authentication.id}/menus/datatableList}]]*/ "";

Solution

  • Your url expression should look like this (what you have doesn't make sense... mismatched brackets and expressions in expressions).

    var ajaxUrl = /*[[@{/api/users/{user}/menus/datatableList(user=${#authentication.id})}]]*/ "";
    

    Also, I'm not even sure that #authentication.id is a valid use of #authentication. Have you verified that it actually contains the id and isn't resolving to null?