0

I have a problem with JSTL and Kotlin interop.

I have a this particular kotlin class with 2 kotlin Boolean fields.

class Foo {
    var isBar1 : Boolean = false
    var isBar2 : Boolean = false

    constructor()

    constructor(isBar1: Boolean, isBar2: Boolean) {
        this.isBar1 = isBar1
        this.isBar2 = isBar2
    }
}

I have to acces these two fields from a jsp which i'm doing in the following way:

<c:choose>
     <c:when test="${foo.isBar1== true}">
          <p>Print something</p>
     </c:when>

     <c:when test="${foo.isBar1== false}">
          <p>Print something else</p>
     </c:when>
</c:choose>

And the exact same for isBar2

The problem is, when I run this piece of code I ran into

java.lang.NoSuchMethodError: packagedeclaration.setBar1(Ljava/lang/Boolean;)V

If i try to write the following functions:

fun getBar1(): Boolean {
    return this.isBar1
}

fun setBar1(isBar1: Boolean): Any {
    this.isBar1= isActive
    return Any()
}

fun getBar2(): Boolean {
    return this.isBar2
}

fun setBar2(isBar2: Boolean): Any {
    this.isBar2= isBar2
    return Any()
}

It will take me to "javax.el.PropertyNotFoundException: Property [isBar2] not found on type [packagedeclaration.foo]"

I've tried to set fields as private but it did not help. Java boolean as parameter seems impossible to pass because IntelliJ constantly bombing me with different errors.

Here's my thoughts: JSTL tries to access java getters/setters with java boolean parameter and return type. Jstl looking for methods named with java naming conventions, so getBar1 and setBar1 but kotlin generates isBar1 and setBar1

Java - Kotlin interop for boolean is Boolean according to this kotlin reference page: Kotlin- Java interop reference

Do you have any idea how to get rid of this problem?

benyuss
  • 104
  • 1
  • 9
  • 1
    Try without "is" `${foo.bar1== true}` for the appropriate matching. – Gurkan Yesilyurt Jan 09 '18 at 14:37
  • 1
    Please have a look at this answer https://stackoverflow.com/questions/43317026/kotlin-naming-convention-for-boolean-returning-methods and see if it helps you in any way. You probably need to use the java getter for your checks inside the JSP template. – Octavian Mărculescu Jan 09 '18 at 14:42
  • @GurkanYesilyurt Thanks for the good tip. Sadly that didn't solve the problem. NoSuchMethodExc with- or without the custom setters (and private/ default visibility modifier too) – benyuss Jan 09 '18 at 14:42
  • 1
    In boolean values, `EL` will look for `isBar1()` getter method for `${foo.bar1}` too. Maybe you have JSTL lib problem. Please remove all JSTL tag and tag lib definition top of your page. Just try with EL `${foo.bar1}` to check it whether is properly being invoked or not. – Gurkan Yesilyurt Jan 09 '18 at 15:31
  • 1
    That was it. Thanks – benyuss Jan 12 '18 at 12:10
  • @GurkanYesilyurt I don't think i can mark a comment as an answer. – benyuss Jan 19 '18 at 09:31
  • @GurkanYesilyurt Thanks, I put a mark on it. – benyuss Jan 19 '18 at 11:18

2 Answers2

1

In boolean values, EL will look for isBar1() getter method for expression ${foo.bar1} too. Probably, you have JSTL lib problem. Please remove all JSTL libs and tag lib definition at the top of your page. Just try with EL ${foo.bar1} to check it whether is properly being invoked or not.

Gurkan Yesilyurt
  • 2,635
  • 2
  • 20
  • 21
0

You can use data class feature of Kotlin here,

Try below

data class Foo(val isBar1:Boolean= false,val isBar2:Boolean= false)
Akhil
  • 6,667
  • 4
  • 31
  • 61