Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically created message reference in Thymeleaf?

Tags:

thymeleaf

So I have the following code:

 <h3 th:if="#{${'footer.message.' + receiptProperties.url}? : '(NOTHING)'}"  th:utext="#{${'footer.message.' + receiptProperties.url}}"></h3>

receiptProperties.url = a name given to a tenant, such as ABC, DEF etc. So the key in the messages.properties file would be something like footer.message.ABC=Hello ABC!

The dynamically created message key displays correctly, however, if the key such as footer.message.GHI does not exist in the properties file, then instead of not displaying anything at all, the following is shown on the page: ??footer.message.GHI_en??

Is there any way in Thymeleaf to accurately check if the dynamically created key exists in the properties file?

like image 291
SiriusBits Avatar asked Apr 05 '17 00:04

SiriusBits


1 Answers

The messages utility object has methods that can be used for this. You could use an expression like:

${#messages.msgOrNull('footer.message.' + receiptProperties.url) == null ? 'Invalid key' : 'Valid key'}
like image 123
Metroids Avatar answered Oct 14 '22 12:10

Metroids