首页 > 解决方案 > Spring Boot:在 Javascript 中访问 Thymeleaf 模型属性得到错误

问题描述

在 Thymeleaf 中使用内联 JavaScript 时出现此错误。此错误显示在我的控制台中..

com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166) ~[jackson-databind-2.10.2.jar:2.10.2] 在 com.fasterxml.jackson.databind.ser.std.CollectionSerializer .serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.10.2.jar:2.10.2]

2020-04-06 12:47:31.262 错误 6764 --- [nio-8080-exec-2] seErrorMvcAutoConfiguration$StaticView:无法呈现请求的错误页面 [/filter] 和异常 [模板解析期间发生错误(模板: “类路径资源 [templates/employeesFiltered.html]”)] 作为响应已经提交。因此,响应可能具有错误的状态代码。

<nav th:replace="layouts :: navbar"></nav>

<div class="container">
    <h3>Employee Directory</h3>
    <hr>

    <!--  Add New Employees Button -->
    <a th:href="@{/employees/new}" class="btn btn-primary btn-sm mb-3">
        Add Employee </a> <input id="txt-search" type="text" class="form-control"
        name="keyword" placeholder="Search...">
    <table class="table table-bordered table-striped">
        <thead class="thead-dark">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="aEmployee : ${employees}">
                <td th:text="${aEmployee.firstName}" />
                <td th:text="${aEmployee.lastName}" />
                <td th:text="${aEmployee.email}" />
            </tr>
        </tbody>

    </table>
</div>

<!-- According to the official documentation: -->

<script th:inline="javascript"> 
/*<![CDATA[*/
var user = /*[[${employees}]]*/;
alert(user);
/*]]>*/
</script>

标签: javaspring

解决方案


推荐阅读