0

How can i use Kotlin extension function in a Thymeleaf template ?

For example, let's take a simple case


    <div th:text="${mystring.test()}">placeholder</div>


where test() is an extension function for String class defined in a Spring Boot kotlin file as


    fun String.test(): String = this


With this code I obtain a SpelEvaluationException

    org.springframework.expression.spel.SpelEvaluationException: 
EL1004E: Method call: Method test() cannot be found on type java.lang.String

org.springframework.boot:spring-boot-starter-thymeleaf version is 2.1.1.RELEASE

Thank you

marco
  • 3,193
  • 4
  • 28
  • 51
  • 1
    First guess is: you can't. Kotlin extension functions are scoped in the Kotlin scope, as soon as it is compiled in bytecode, this does not really exist, so the "Java world" cannot use a Kotlin extension function. I'm going to check it and if I get concrete proofs, I'm going to post you an answer. – sjahan Jan 18 '19 at 14:43
  • Possible duplicate of [Accessing Kotlin extension functions from Java](https://stackoverflow.com/questions/28294509/accessing-kotlin-extension-functions-from-java) I think you should find the proper explanation here. – sjahan Jan 18 '19 at 14:45
  • I guessed it too, but I would have liked if it had been possible declare some dedicated method for the 'view side' of application. If you post your first comment as an answer, i'll accept it. Thank you – marco Jan 22 '19 at 21:35
  • Just hit this problem..... wish it was ;) – szxnyc Feb 13 '19 at 21:50

0 Answers0