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

When using Gretty with the Thymeleaf Layout Dialect, an exception was thrown when visiting a layout-applied page.

mklinkj opened this issue · comments

Hello.

When running the web server with Gretty.. When accessing the layout page, the following exception is exposed and nothing appears on the web browser screen.

  • Exception Log

    Caused by: java.lang.ClassCastException: class nz.net.ultraq.thymeleaf.layoutdialect.models.extensions.EventIterator cannot be cast to class [Ljava.lang.Object; (nz.net.ultraq.thymeleaf.layoutdialect.models.extensions.EventIterator is in unnamed module of loader java.net.URLClassLoader @889d9e8; [Ljava.lang.Object; is in module java.base of loader 'bootstrap')
            at org.codehaus.groovy.runtime.dgm$236.doMethodInvoke(Unknown Source) ~[groovy-4.0.10.jar:4.0.10]
            at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321) ~[groovy-4.0.10.jar:4.0.10]
            at nz.net.ultraq.thymeleaf.layoutdialect.models.extensions.IModelExtensions.find(IModelExtensions.groovy:117) ~[thymeleaf-layout-dialect-3.2.1.jar:?]
            ...
    

My project environment looks like this

  • Spring 6 (Not Spring Boot )
  • Thymeleaf
    • thymeleaf-spring6: 3.1.1.RELEASE
    • thymeleaf-layout-dialect: 3.2.1
  • Gretty 4.0.3
    • Tomcat 10.1.10

When I run the web project, I run it with the command below.

gradle clean appRun

When I ran Gretty and accessed the page with the layout applied, I definitely saw the aforementioned exception.

However, there are two things that are unusual.

✨ Two unusual things

  1. If I'm using a direct deployment of Tomcat without using Gretty, I don't get the exception.

  2. If I use https://github.com/zhanhb/thymeleaf-layout-dialect, there's no problem running a web project with Gretty.

      // TODO: Using gretty with thymeleaf-layout-dialect causes an exception
      implementation "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:${thymeleafLayoutVersion}"
      // TODO: If I use the thymeleaf-layout-dialect modified by zhanhb, the exception does not occur.
      // implementation "com.github.zhanhb:thymeleaf-layout-dialect:${zhanhbThymeleafLayoutVersion}"

✨ Example project to see the problem

I've created an example project for you to see the issue.

Thank you. Have a nice day. 👍

Hey there, sorry to take so long getting back to you on this one as this is an interesting bug and had me stumped for a very long time. However, I gave it another look this weekend and found the issue.

The Gretty plugin is written against Groovy 3, and introduces Groovy 3 JARs into the classloader of the servlet container that is being launched (Tomcat 10 in this case). However, version 3.1.0 and newer of the Thymeleaf Layout Dialect is written in Groovy 4, and so this mixup of Groovy versions causes the issues that you're seeing.

The only workarounds I can think of right now are:

  • to run Tomcat directly (as you've found, this doesn't cause the issue)
  • to use the Java-only port of the layout dialect by zhanhb
  • to use a version of the layout dialect before the move to Groovy 4, like version 3.0.0 (I've tested this using your example project and it works)
  • or, since you're using Spring, to use something like Spring Boot so that Tomcat can be bundled with your project and also includes a Gradle plugin to use in your build file

I hope that gives you some options to consider. Thank you again for the bug report and the example project which helped me figure out what was going on 🙇‍♂️

Thanks for your answer.

I posted the question in two places, so there’s also an issue on Gretty’s Github. 😅

gretty-gradle-plugin/gretty#289

Have a nice day. 👍