th:if comparing objects

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

th:if comparing objects

mausumi
Hi,

I want to compare two objects.Please how can i do it..

<div th:object="${product}">
                <div th:each="entry: ${product.productAttributes}">
                        <div th:if="${entry.key == 'shipping'}">
                               

<div th:if="${item['contentName'] == ${entry.value.value}">
                                                <div th:utext="${item[htmlContent]}"></div>
                                        </div>


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

I am trying to do as above but i am getting error in th:if line...Please help getting this work.

Thanks,
Mausumi
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

eiryu
My anticipation is this.
<div th:if="${item['contentName'] == entry.value.value}">
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

Emanuel
Administrator
In reply to this post by mausumi
I think there's also a syntax error in this line:

th:if="${item['contentName'] == ${entry.value.value}"

You've got an extra ${ inside an ${} expression.  Try removing the ${ next to entry.value.value
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

Antibrumm
Also take care using == .

Like always in java this does just a memory address comparison for objects and as far as i know thymeleaf is not treating this expression as an equal call. (Which could be a possible improvement? ;)

So you really should call ${object1.equals(object2)} in your statements.
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

danielfernandez
Administrator
Antibrumm wrote
Also take care using == .

Like always in java this does just a memory address comparison for objects and as far as i know thymeleaf is not treating this expression as an equal call. (Which could be a possible improvement? ;)

So you really should call ${object1.equals(object2)} in your statements.
There shouldn't really be a need for that. But anyway, there are two possible scenarios: The equality operation this thread is about is not being executed by Thymeleaf, but by the expression engine itself (OGNL or SpringEL, depending on whether mausumi is using Spring or not). Note the "==" is inside the ${...}:
<div th:if="${entry.key == 'shipping'}">

Both OGNL and SpringEL will use object equality and not reference equality, so you should be fine.

Second scenario: thymeleaf allows you to perform equality expressions outside variable expressions, like:
<div th:if="${entry.key} == #{shipping.value}">

...and in that case, it would be thymeleaf the one in charge of executing that equality. And Thymeleaf also performs object equality (not reference). See https://github.com/thymeleaf/thymeleaf/blob/2.1-master/src/main/java/org/thymeleaf/standard/expression/EqualsExpression.java

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

Antibrumm
Hmm, that sound logical.

However, I am pretty sure i needed to use equals explicitly once in my templates exactly because the method was not translated. I will have to check my code and will report back and correct myself if it is indeed working. I'm using spring mvc.
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

Antibrumm
I had finally some time to check this one out and i can confirm the behavior i described.

The issue is comming from the spring side where the OpEQ class is not using equals unless the left side is a comparable!? And in other cases just returns

BooleanTypedValue.forValue(left == right);

This seems to work for them but in my case where i have hibernate proxies this doesnt. Someone can confirm this?

Spring 3.2.3
Entity = object with equals method on a uuid field
Two instances with same uuid.

th:if="${entity1} == ${entity2}" -> true

th:if="${entity1.equals(entity2)}" -> true

th:if="${entity1 == entity2}" -> false


Objections?

Anti
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

danielfernandez
Administrator
Interesting. I wonder why Spring doesn't perform an equals() even if objects are not Comparable...
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

Antibrumm
Yes. Pretty strange implementation :) havent seen an EL which doesnt implement equals as a last resort.
Even found a jira entry. Vote for it!

https://jira.springsource.org/browse/SPR-9194
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

Antibrumm
Oh and the "NOT comparable" was a typo...
Reply | Threaded
Open this post in threaded view
|

Re: th:if comparing objects

huqim
In reply to this post by danielfernandez
it works, thank you