ultraq / thymeleaf-layout-dialect

A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse

Home Page:https://ultraq.github.io/thymeleaf-layout-dialect/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

java.lang.ClassCastException

gmatege opened this issue · comments

Just upgraded to version 3.0.0 (from 1.2.4) and having this exception whenever am processing a template:

class nz.net.ultraq.thymeleaf.layoutdialect.models.extensions.EventIterator cannot be cast to class java.util.Map (nz.net.ultraq.thymeleaf.layoutdialect.models.extensions.EventIterator is in unnamed module of loader java.net.URLClassLoader @a9c5b75; java.util.Map is in module java.base of loader 'bootstrap')...

Currently, running on JDK 14 (Note: I have already tried 2.x.x and even more complications).

Hmm, haven't encountered this before, but the error message sounds like you're running the layout dialect as a Java module on the module path instead of the classpath? I only just gave it an automatic module name in 3.x.

Also, are you able to share a bit more of the stack trace? Especially any lines in the layout dialect where it might be trying to cast EventIterator into a Map? I can go looking through the codebase and make some guesses but anything more concrete will help.

Tried to run more features, for example:

data-th-insert="~{shared/contentIntro}" data-layout-insert="~{shared/contentIntro}"

Those two are supposed to do exactly the same thing, BUT the first one works as expected and the second doesn't, Here is part of the stacktrace:

Nov 18, 2021 10:52:55 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/bargain] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'nz.net.ultraq.thymeleaf.includes.InsertProcessor' (template: "index/home" - line 21, col 10)] with root cause
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at org.codehaus.groovy.runtime.dgm$1125.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1217)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041)
at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:999)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:990)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToBoolean(DefaultTypeTransformation.java:197)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.booleanUnbox(DefaultTypeTransformation.java:86)
at nz.net.ultraq.thymeleaf.expressions.ExpressionProcessor.parseFragmentExpression(ExpressionProcessor.groovy:68)
at nz.net.ultraq.thymeleaf.expressions.ExpressionProcessor$parseFragmentExpression.call(Unknown Source)
at nz.net.ultraq.thymeleaf.includes.InsertProcessor.doProcess(InsertProcessor.groovy:69)
at org.thymeleaf.processor.element.AbstractAttributeModelProcessor.doProcess(AbstractAttributeModelProcessor.java:77)
at org.thymeleaf.processor.element.AbstractElementModelProcessor.process(AbstractElementModelProcessor.java:98)
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementModelProcessorWrapper.process(ProcessorConfigurationUtils.java:649)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1510)
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205)
at org.thymeleaf.engine.Model.process(Model.java:282)
at org.thymeleaf.engine.Model.process(Model.java:290)
at org.thymeleaf.engine.GatheringModelProcessable.process(GatheringModelProcessable.java:78)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640)
at org.thymeleaf.engine.CloseElementTag.beHandled(CloseElementTag.java:139)
at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366)
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:190)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1397)

Somewhere here:
at nz.net.ultraq.thymeleaf.expressions.ExpressionProcessor.parseFragmentExpression(ExpressionProcessor.groovy:68)
at nz.net.ultraq.thymeleaf.expressions.ExpressionProcessor$parseFragmentExpression.call(Unknown Source)
Might be the problem because it transforms the expression in some way

As for the original exception, part of the stacktrace is:

java.lang.ClassCastException: class nz.net.ultraq.thymeleaf.models.extensions.EventIterator cannot be cast to class [Ljava.lang.Object; (nz.net.ultraq.thymeleaf.models.extensions.EventIterator is in unnamed module of loader java.net.URLClassLoader @a9c5b75; [Ljava.lang.Object; is in module java.base of loader 'bootstrap')
org.codehaus.groovy.runtime.dgm$232.invoke(Unknown Source)
org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoMetaMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:223)
org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:70)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
nz.net.ultraq.thymeleaf.models.extensions.IModelExtensions.find(IModelExtensions.groovy:125)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:564)
org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:54)
org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:54)
org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:244)
org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
nz.net.ultraq.thymeleaf.decorators.DecorateProcessor.doProcess(DecorateProcessor.groovy:103)

It seems the casting is being carried out in Groovy.

The errors were related to using the Groovy version of this project in a Java project, All went away after switching to the Java version of this project at:

https://github.com/zhanhb/thymeleaf-layout-dialect

also facing same problem but no encouraging answer found