Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf aggregation not working

We've got the following code:

<div th:each="client : ${clients}">
    <div th:each="purchase : ${client.purchases}">
        <div th:each="product : ${purchase.products}">
            <span th:text="${product.price}"></span>
            <!-- <span id="2" th:text="${#aggregates.sum(products.{price})}"> </span> -->
            <!-- <span id="2" th:text="${#aggregates.sum(products.![price])}"> </span>  -->
        </div>
    </div>
</div>

The output is:

5.25
4.20

If I uncomment the first comment, I get error

Exception evaluating SpringEL expression: "#aggregates.sum(products.{price})" (clients/clients:84)

If I uncomment only the second comment, I get error:

Exception evaluating SpringEL expression: "#aggregates.sum(products.![price])" (clients/clients:85)

I tried to use http://demo-dspace-cris.cineca.it/bitstream/123456789/26/1/usingthymeleaf.pdf

I am using thymeleaf 2.1.4

IT WORKS !

I should use:

<span id="2" th:text="${#aggregates.sum(purchase.products.![price])}"> </span> 
like image 263
Kamil Witkowski Avatar asked Apr 29 '16 13:04

Kamil Witkowski


People also ask

How does thymeleaf work?

Thymeleaf works thanks to a DOM processing engine and a series of processors —one for each type of node that needs to apply logic— that modify the document’s DOM tree in order to create the results you expect by combining this tree with your data.

How to display errors in thymeleaf templates?

Let's construct the templates piecemeal based on what type of errors we can display. 3.1. Displaying Field Errors Thymeleaf offers an inbuilt field.hasErrors method that returns a boolean depending on whether any errors exist for a given field. Combining it with a th:if we can choose to display the error if it exists:

What if ${condition} is false in thymeleaf?

In this case, if $ {condition} is false, null will be returned, and thus no removal will be performed. Thymeleaf calls local variables those variables that are defined for a specific fragment of a template, and are only available for evaluation inside that fragment. An example we have already seen is the prod iter variable in our product list page:

Can thymeleaf process XML files?

It is better suited for serving XHTML/HTML5 in web applications, but it can process any XML file, be it in web or in standalone applications. The main goal of Thymeleaf is to provide an elegant and well-formed way of creating templates.


1 Answers

I figured it myself after posting here !

<div th:each="client : ${clients}">

            <div th:each="purchase : ${client.purchases}">
                <span id="2"
                    th:text="${#aggregates.sum(purchase.products.![price])}"> </span>
                <div th:each="product : ${purchase.products}">
                    <span th:text="${product.price}"></span>

                </div>
            </div>
        </div>

output:

9.45
5.25
4.20

site that helped me a lot: http://forum.thymeleaf.org/aggregates-with-Spring-td3767446.html

like image 88
Kamil Witkowski Avatar answered Sep 28 '22 10:09

Kamil Witkowski