thymeleaf 一个莫名其妙的错误提示 org.attoparser.ParseException

开发者在使用Thymeleaf时遇到莫名其妙的错误,源于页面HTML中的特殊字符导致的解析问题。通过在script标签内使用CDATA块包含JS代码,解决了该重复属性标记的问题。

thymeleaf 一个莫名其妙的错误提示

介绍

开发过程中遇到一个莫名奇妙的错误,一时竟然不知道怎么解决,找官网也没有找到

问题

页面显示

在这里插入图片描述

错误日志

org.attoparser.ParseException: (Line = 96, Column = 5) Malformed markup: Attribute “}” appears more than once in element

其实就是说多了一个 }

看下我的代码位置

在这里插入图片描述

这么看代码是没有任何问题的,而且直接打开到浏览器也是没有问题的

完整错误日志

2024-03-01 09:12:02.395 ERROR 8372 --- [p-nio-80-exec-5] 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.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/my/d.html]" - line 96, col 5)] with root cause

org.attoparser.ParseException: (Line = 96, Column = 5) Malformed markup: Attribute "}" appears more than once in element
	at org.attoparser.MarkupEventProcessorHandler.handleAttribute(MarkupEventProcessorHandler.java:549) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.attoparser.ParsingAttributeSequenceUtil.parseAttributeSequence(ParsingAttributeSequenceUtil.java:220) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:153) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_291]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_291]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.36.jar:9.0.36]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_291]


解决

在一筹莫展的时候找到了一个帖子处理其他特殊字符问题的,说是要加一个这个才行

	<script>
	//<![CDATA[
	....
	//]]>
	<script/>

在script 中加上它,把所有的js代码全部包括起来,果断试一试

结果

刷新后…

成功

页面展示

在这里插入图片描述

<think>我们正在处理一个Spring模板解析异常,具体是关于EL表达式`#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')`在模板admin第151行第25列。异常类型为`org.attoparser.ParseException`。 根据之前的引用,我们并没有直接关于这个异常的信息,但是我们可以根据常见的Spring模板解析问题来推断。 首先,注意这个表达式使用了Thymeleaf的日期格式化工具(`#dates`)。根据引用[3],我们知道这个项目使用了Thymeleaf(因为提到了th:text等指令)。异常发生在解析阶段,因此可能是语法错误或者表达式中的某些部分不符合Thymeleaf的解析规则。 观察表达式:`#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')`。注意,在Thymeleaf中,表达式的字符串参数应该用单引号或双引号括起来,但是这里在字符串`'yyyy-MM-dd HH:mm'`中,我们使用了单引号。然而,由于整个表达式是写在Thymeleaf的属性中(比如`th:text`),通常属性值是用双引号括起来的,所以内部使用单引号是允许的。但是,请注意表达式中的单引号嵌套可能会引起问题。 但是,异常信息指出在第151行第25列,因此我们需要检查模板admin中该位置附近的代码。 可能的错误原因及解决方案: 1. **单引号嵌套问题**:如果表达式所在的属性值本身是用单引号括起来的,那么表达式内部的单引号就需要转义,或者改用双引号。例如: - 错误情况:`th:text='#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')'` - 这里,属性值使用单引号,而内部格式字符串也使用单引号,导致解析器会认为属性值在第一个`'yyyy`之前的单引号就结束了,从而引发解析错误。 解决方案: - 将外层的单引号改为双引号:`th:text="#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')"` - 或者将内部单引号转义:`th:text='#dates.format(registration.registrationTime, \'yyyy-MM-dd HH:mm\')'` 2. **表达式语法错误**:检查表达式是否完整,变量是否存在。确保`registration`对象及其`registrationTime`属性在上下文中存在,并且不为null(否则可能会导致格式化异常,但这里报的是解析异常,所以更可能是语法问题)。 3. **日期格式字符串错误**:确认日期格式字符串是否正确。这里`'yyyy-MM-dd HH:mm'`是正确的,但注意HH代表24小时制。 4. **Thymeleaf版本问题**:不同版本的Thymeleaf对表达式的支持可能有差异,确保使用的版本支持`#dates`工具对象。 5. **表达式位置错误**:确保表达式只出现在Thymeleaf允许的地方,比如文本替换(th:text)、属性值等。 由于异常是`ParseException`,说明在解析模板时遇到不符合语法规则的结构,因此我们应重点检查模板中该位置的语法。 根据引用[3]中关于Thymeleaf的描述,我们还可以注意到,Thymeleaf能够很好地处理模板中的静态和动态内容,即使表达式解析失败,也会显示默认的静态内容。但这里是在解析阶段就发生了异常,所以模板引擎无法继续。 因此,请按照以下步骤检查和修复: - 定位到admin模板的第151行,查看包含该表达式的标签及其属性。 - 检查属性值的引号使用是否正确,避免嵌套引号未转义。 - 如果表达式很长,可以尝试拆分成多个部分,或者使用`th:with`先定义变量。 例如,如果原来的代码是: ```html <span th:text='#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')'></span> ``` 应改为: ```html <span th:text="#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')"></span> ``` (外层用双引号,内层用单引号) 或者: ```html <span th:text='#dates.format(registration.registrationTime, "yyyy-MM-dd HH:mm")'></span> ``` (外层单引号,内层双引号) 如果问题仍然存在,请提供模板中该行的完整代码,以便进一步分析。 另外,引用[2]中提到了Spring的SpEL表达式,但这里使用的是Thymeleaf的表达式,所以引用[2]可能不直接相关。 综上所述,最可能的原因是引号嵌套问题,请优先检查并调整引号的使用。 </think>### 解决 Spring EL 表达式解析异常的方法 您在 Thymeleaf 模板中遇到的 `org.attoparser.ParseException` 异常通常是由于 EL 表达式语法问题或日期格式配置错误导致的。以下是完整解决方案: #### 1. **检查引号嵌套问题(最常见原因)** 问题分析: 表达式 `#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')` 中的单引号嵌套会导致解析失败,尤其在属性值使用单引号包裹时。 解决方案: **方案A:外层使用双引号** ```html <td th:text="#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')"></td> ``` **方案B:转义内部单引号** ```html <td th:text='#dates.format(registration.registrationTime, \'yyyy-MM-dd HH:mm\')'></td> ``` #### 2. **验证日期格式有效性** 问题分析: `HH` 表示 24 小时制,但部分 Thymeleaf 版本需要验证格式兼容性。 解决方案: 改用更兼容的 ISO 格式: ```html <td th:text="${#temporals.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')}"></td> ``` 或简化格式: ```html <td th:text="${#dates.format(registration.registrationTime, 'yyyy-MM-dd')}"></td> ``` #### 3. **检查 registrationTime 是否为 null** 问题分析: 如果 `registration.registrationTime` 为 null,会触发解析异常。 解决方案: 添加空值检查: ```html <td th:text="${registration.registrationTime != null} ? ${#dates.format(registration.registrationTime, 'yyyy-MM-dd HH:mm')} : 'N/A'"></td> ``` #### 4. **确认 Thymeleaf 版本兼容性** 执行步骤: 1. 检查 `pom.xml` 中 Thymeleaf 版本: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>3.1.0</version> <!-- 建议≥3.0.0 --> </dependency> ``` 2. 升级到最新版可解决历史版本中的日期解析缺陷 #### 5. **验证对象属性路径** 问题分析: `registration.registrationTime` 路径错误会导致解析失败。 调试方法: ```html <!-- 临时调试代码 --> <div th:text="${registration}"></div> <!-- 检查对象是否存在 --> <div th:text="${registration?.registrationTime}"></div> <!-- 检查时间属性 --> ``` #### 6. **配置时区处理(关键步骤)** 在 `application.properties` 添加: ```properties spring.thymeleaf.servlet.context-parameters.spring.application.timezone=GMT+8 ``` 确保服务器时区与格式化时区一致[^1]。 #### 7. **检查模板语法完整性** 在 admin 模板第 151 行附近验证: - 标签是否闭合 (如 `<td></td>`) - 是否缺少 `th:object` 声明 - 特殊符号转义是否正确 ### 最终修复方案推荐 ```html <!-- 使用推荐方案 --> <td th:text="${#temporals.format(registration?.registrationTime, 'yyyy-MM-dd HH:mm')} ?: '未登记'"></td> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值