Thymeleaf 3 #request对象在内联Javascript中为null

时间:2018-04-26 18:48:16

标签: thymeleaf

我在Spring Boot 2网络应用程序的网页中有以下代码:

<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抛出错误,表示无法在null正确获取语言环境时获取nullUR。 Thymeleaf 3官方文档称#request和#locale是Web上下文中的有效对象。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

Metroids提及并参考Spring boot入门Securing a Web Application用户#httpServletRequest不为null。是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>