2

I have below code in a web page in Spring Boot 2 web app:

<div data-th-fragment="head-section">
<div> blah </div>
<script data-th-inline="javascript">
  var url = [[${#request.requestURL} ]];
  var country = [[${#locale.country}]]

  </script>

</div>

Thymeleaf throws error that says cannot get requestURL on null while it correctly gets the locale. Thymeleaf 3 official documentation says #request and #locale are valid objects in web context.

How to fix this issue?

ace
  • 11,526
  • 39
  • 113
  • 193
  • Have you tried retrieving a request parameter, like so, just for testing, and does it also fail? ${#request.getParameter('q')} – TinkerTenorSoftwareGuy Apr 26 '18 at 18:58
  • 1
    Are you sure you're using Thymeleaf 3? Your example worked for me when I tried it. Does `var url = [[${#httpServletRequest.requestURL}]];` work? – Metroids Apr 26 '18 at 19:11
  • Thank you for comments. Yes I tried that. From stacktrace it is thymeleaf version 3.0.9. both #request and httpServletRequest are null. – ace Apr 27 '18 at 05:27

1 Answers1

2

Just Metroids mention and Refer to Spring boot getting started Securing a Web Application User #httpServletRequest is not null. is an instance of HttpServletRequestWrapper

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
        <form th:action="@{/logout}" method="post">
            <input type="submit" value="Sign Out"/>
        </form>
    </body>
</html>
billschen
  • 699
  • 6
  • 11