Spring Boot:在JavaScript中访问Thymeleaf模型属性时出现错误

huangapple 未分类评论47阅读模式
英文:

Spring Boot: Accessing Thymeleaf Model Attributes in Javascript getting the Error

问题

Getting this Error...while using inline JavaScript in Thymeleaf.
This Error is showing in my console..

com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166) ~[jackson-databind-2.10.2.jar:2.10.2]
	at 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 ERROR 6764 --- [nio-8080-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/filter] and exception [An error happened during template parsing (template: "class path resource [templates/employeesFiltered.html]")] as the response has already been committed. As a result, the response may have the wrong status code.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head th:replace="layouts :: header"></head>

<body>

	<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>
</body>

</html>
英文:

Getting this Error...while using inline JavaScript in Thymeleaf.
This Error is showing in my console..

com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166) ~[jackson-databind-2.10.2.jar:2.10.2]
at 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 ERROR 6764 --- [nio-8080-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/filter] and exception [An error happened during template parsing (template: "class path resource [templates/employeesFiltered.html]")] as the response has already been committed. As a result, the response may have the wrong status code.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head th:replace="layouts :: header"></head>

<body>

&lt;nav th:replace=&quot;layouts :: navbar&quot;&gt;&lt;/nav&gt;

&lt;div class=&quot;container&quot;&gt;
	&lt;h3&gt;Employee Directory&lt;/h3&gt;
	&lt;hr&gt;

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

	&lt;/table&gt;
&lt;/div&gt;

&lt;!-- According to the official documentation: --&gt;

&lt;script th:inline=&quot;javascript&quot;&gt; 
/*&lt;![CDATA[*/
var user = /*[[${employees}]]*/;
alert(user);
/*]]&gt;*/
&lt;/script&gt;

</body>

</html>

huangapple
  • 本文由 发表于 2020年4月6日 17:01:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/61056237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定