Vicky Rathee
Vicky Rathee

Reputation: 69

Fetch value from thymeleaf message expression using # and doing null check

I am fetching values from an XML template using #. but when i am fetching value for a label which is not available. i'm getting its property name as label name. I want to set a default value if that label is not available in xml file.

<th:block th:replace="fragments/header :: nav-header-back-text(header_txt=
 #{label.btn.unsubscribe+'_'+${operator_id}} ?  
#{label.btn.unsubscribe+'_'+${operator_id}} : #{label.btn.unsubscribe})">

lets say operator_id is com, then i want if label.btn.unsubscribe_com is available then show it otherwise show value of label.btn.unsubscribe from message resource, but i am getting something like ??label.btn.unsubscribe+'_'+${operator_id}??.

i am using thymeleaf with spring boot and above code is written inside html file.

Upvotes: 0

Views: 418

Answers (1)

Metroids
Metroids

Reputation: 20477

You can't build strings like that in #{...} expressions. Instead you need to use the #messages helper. Something like this should work. I don't have all your messages set up, but something like this should work:

<th:block th:replace="fragments/header :: nav-header-back-text(header_txt=${#messages.msgOrNull('label.btn.unsubscribe_' + operator_id)} ?: #{label.btn.unsubscribe})">

Upvotes: 1

Related Questions