Search code examples
spring-bootthymeleafspring-el

Spring boot and thymeleaf: Can not evaluate size() of an ArrayList


I have an ArrayList in my mvc controller class:

List<Market> markets = loadMarkets();

I add this list to Model object:

model.addAttribute("markets", markets);

In my template, when I want to get size() of this array:

<span>Top <strong th:text="${markets.size}"></strong>assets.</span>

I got this error:

Cannot evaluate because of compilation error(s): The method size() is undefined for the type Object.

Snapshot of the VSCode debugging tools:

enter image description here

I am using Spring Boot version 2.2.6.RELEASE with Thymeleaf template engine.


Solution

  • When you use property notation th:text="${markets.size}", then Thymeleaf will search for a method getSize() on the markets attribute in the model. But there is no getSize() method, only size() method.

    So use the method itself instead of using the property notation:

    th:text="${markets.size()}"