3

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?

3
  • Have you tried retrieving a request parameter, like so, just for testing, and does it also fail? ${#request.getParameter('q')} Commented Apr 26, 2018 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? Commented Apr 26, 2018 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. Commented Apr 27, 2018 at 5:27

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.