spring boot 的常见thymeleaf异常之 Could not parse as expression 错误

xiaoxiao2025-04-25  5

今天在前端使用easy框架,然后遇到这样一个错误:

ERROR 12788 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "

前端页面显示的是:

控制台显示的是:

我就很奇怪,报错的{field: 'id', title: '编号', width: 50},这些是前端javascript的代码,为什么报500的错误,如下:

我尝试将代码注释掉结果还是一样,

可见不是运行的问题,而是编译的问题,template包下的页面都会被spring先编译一遍,因为是thymeleaf抛出的异常后来在网上查了一些thymeleaf的信息,而这里之所以 报错是因为thymeleaf会将[[......]]当做他的内联表达式,而这些代码当中并不是表达式,所以才会抛出异常说不能解析为表达式,真是坑爹啊,跟多维数组也就是[[...],[...]]的形式差不多

thymeleaf内联表达式可以参考这篇文章https://blog.csdn.net/dong_19890208/article/details/61914964

解决的方式很简单,在script标签中加入th:inline="none",就可以不用解析成表达式了,例如:<script th:inline="none"> </script>

不过也可以在其间插入换行破坏掉[[,例如:

[ [ {field: 'id', title: '编号', width: 50}, {field: 'account', title: '账号', width: 200}, {field: 'password', title: '密码', width: 200} ] ]

 涉及改动大的话推荐使用前者

转载请注明原文地址: https://www.6miu.com/read-5029072.html

最新回复(0)