提问者:小点点

Thymeleaf 3#请求对象在内联Javascript中为空


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

<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抛出错误,指出当它正确获取语言环境时,无法将request estURL设置为null。Thymeleaf 3官方留档说#request和#locale是Web上下文中的有效对象。

如何解决这个问题?


共1个答案

匿名用户

只是Metroids提到并参考Spring boot入门保护Web应用程序用户#httpServletRequest不为空。是HttpServletRequest estWrapper的一个实例

<!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>