spring-el

how to invoke a method from Spring EL


scenario

Car car= new Car()
car.setRegNumber("12345");

//check if the last 3 digit is matching
String expresionString="( Math.abs(regNumber) % 1000  == 345 )"

But the above expression fails with error "property or field 'Math' cant be found on object Car'. Ss there another way instead of using string conversion/substring.


Solution

  • I would use the matches operator:

    String expresionString="( regNumber matches '.*345$' )"
    

    It matches if the last 3 disgits are 345

    or simply

    String expresionString="( regNumber.endsWith('345') )";
    

    if you want to use static methods like in your example, you have to put the classname into T(..)

    String expresionString="T(Math).abs(T(Integer).valueOf(regNumber)) % 1000  == 345";