我有一个基本的Spring Boot 2.0.4.RELEASE应用程序。使用Spring Initializr,JPA,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件。
我在模板中定义了数据表
<script th:inline="javascript">
/*<![CDATA[*/
$.fn.dataTable.ext.errMode = 'throw';
var ajaxUrl = /*[[@{${ajaxUrl}}]]*/ "";
var table = $('#smsEventTable').DataTable( {
order: [[ 0, "desc" ]],
select: true,
bLengthChange: false,
stateSave: true,
pageLength: 20,
ajax: ajaxUrl,
"columns": [
{ data: 'id' },
{ data: 'smsId' },
{ data: 'companyName' },
{ data: 'description' },
{ data: 'battery', className: 'col_battery' },
{ data: 'dateTime' },
{ data: 'signal' },
{ data: 'data' },
{ data: 'alarm' }
]
});
/*]]>*/
</script>
但是第一次加载页面时没有数据我收到了这个错误:
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: " 0, "desc" " (template: "/sms/smsList" - line 273, col 12)
这里的问题是这个表达式:order: [[ 0, "desc" ]],
。由于该表达式具有双括号([[
和]]
),Thymeleaf正试图将其评估为inline expression。解决这个问题的最简单方法是通过分解括号来格式化它:
order: [ [0, "desc"] ],
要么
order: [
[0, "desc"]
],
您还可以将JavaScript拆分为两个块(在第二个块中禁用内联),如下所示:
<script th:inline="javascript">
var ajaxUrl = /*[[@{${ajaxUrl}}]]*/ "";
</script>
<script th:inline="none">
/*<![CDATA[*/
$.fn.dataTable.ext.errMode = 'throw';
var table = $('#smsEventTable').DataTable({
order: [[0, "desc"]],
select: true,
bLengthChange: false,
stateSave: true,
pageLength: 20,
ajax: ajaxUrl,
"columns": [
{data: 'id'},
{data: 'smsId'},
{data: 'companyName'},
{data: 'description'},
{data: 'battery', className: 'col_battery'},
{data: 'dateTime'},
{data: 'signal'},
{data: 'data'},
{data: 'alarm'}
]
});
/*]]>*/
</script>