Spring Boot整合Thymeleaf:Could not parse as expression

问题 

项目使用springboot整合Thymeleaf在页面定义数组时遇到问题,数组格式为

var d = [[123456,2],[156449,3]]

 运行后控制台报错

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "1262304000000, 6], [1264982400000, 3057], [1267401600000, 20434], [1270080000000, 31982], [1272672000000, 26602], [1275350400000, 27826], [1277942400000, 24302], [1280620800000, 24237], [1283299200000, 21004], [1285891200000, 12144], [1288569600000, 10577], [1291161600000, 10295" (template: "/ui/widgets" - line 564, col 24)

原因

Thymeleaf中的内联表达式允许直接在 HTML 文本中使用标准表达式,[[]]相当于th:text,对含有 HTML 标签的内容自动进行字符转义。

根据控制台打印的日志可以看出, Thymeleaf把"[["和"]]"之间的内容直接转化展示,所以报错无法解析表达式

解决方法

Thymeleaf的th:inline

描述
none禁止内联表达式,可以原样输出 [[]] 和 [()] 字符串
text文本内联,可以使用 th:each 等高级语法
css样式内联,如:<style th:inline="css">
javascript脚本内联,如:<style th:inline="javascript">

none

<!-- [[1, 2], [3, 4]] -->
<p th:inline="none">[[1, 2], [3, 4]]</p>

<script th:inline="none">
    var d = [[1, 2], [3, 4]];
</script>

 text

<!-- 北京 上海 广州 深圳 -->
<p th:inline="text">
    [# th:each="city : ${cities}"]
        [(${city.name})]
    [/]
</p>

css

<style th:inline="css">
    body {
        background-color:[[${bgColor}]];
    }
</style>

javascript

<script th:inline="javascript">
    var user = [[${user}]];
    alert("用户名:" + user.name);
</script>

使用none,即在标签中加入th:inline="none"

thymeleaf参考资料:https://fanlychie.github.io/post/thymeleaf.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值