Quantcast

Determining the type of a instance at the Thymeleaf template level or Tiles def level.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Determining the type of a instance at the Thymeleaf template level or Tiles def level.

balteo
Hello,

I have decided to go for JPA entity inheritance in the project I am currently working on.

Thus, I have an abstract Member entity class that is subclassed by two other entities, Male and Female.

I would like to be able to use the common attributes from the Member entity class wherever possible and when the attributes differ, use the Male or Female entity classes.

My question is as follows:

1. Is it better to deal with those differences at the Tiles definition level or at the Thymeleaf template level?
2. How do I determine whether a member is an instance of Male or Female at the Tiles definition level (still using Thymeleaf syntax of course and using thymeleaf-extras-tiles2)?
3. How do I determine whether a member is an instance of Male or Female at the Thymeleaf template level?

Best regards,

Julien.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Determining the type of a instance at the Thymeleaf template level or Tiles def level.

Zemi
Administrator
Hi,

I know nothing about Tiles, but for the Thymeleaf part, you can see 3 different approaches in this thread:
    http://forum.thymeleaf.org/Testing-for-instanceof-td4024921.html

Even simpler than that, you could add some boolean methods to your Member base class:
    abstract boolean isMale();
    abstract boolean isFemale();
This is uglier in the Java code, but it looks nice at the template

    <div th:if="${member.male}">
         ...
    </div>

and it is more robust than typing the className into the template (think of some renaming or package change of the class).
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Determining the type of a instance at the Thymeleaf template level or Tiles def level.

balteo
1. Thanks Zemi. I am definitely going to use the approach with the two abstract methods.
2. I would be grateful if someone could provide advice about the other part of my question: how to deal with types in Apache Tiles for Thymeleaf??
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Determining the type of a instance at the Thymeleaf template level or Tiles def level.

AixNPanes
You can just use java:

${object.getClass().getName()}

or more simply

${object.class.name}
Loading...