I am using spring web flow 2.5.0
along with thymeleaf 3.0.9
.
I have a class containing some static keys ex:
public class MyKeys{
.....
public static final String myKey = "myKey1"
.....
}
Somewhere in my controller I store a value for that key in order to be available in the template file.
(pseudo-code):
context.add(MyKeys.mykey,"screen.home.welcome");
screen.home.welcome
is an i18n message key(store on application's message.properties
) whose value I want to present to the user.
This works but I want to use the key from MyKeys
class to access its value.
<div th:utext="#{${myKey1}}"></div>
What I have tried and is not working:
<div th:utext="#{${T(com.package.MyKeys).myKey}}"></div>
With this what I get in the template is myKey1
. How I can instruct thymeleaf to retrieve the value which is associated to myKey1
?
That level of indirection you want is a little weird... the reason this doesn't work:
<div th:utext="#{${T(com.package.MyKeys).myKey}}"></div>
// Resolves to
<div th:utext="#{myKey1}"></div>
// Which isn't the actual message key since it's supposed to look like this:
<div th:utext="#{screen.home.welcome}"></div>
I probably wouldn't recommend it, but you could probably use preprocessing for this. Something like this would probably work for you:
<div th:utext="#{${__${T(com.package.MyKeys).myKey}__}}"></div>
// Resolves to
<div th:utext="#{${myKey1}}"></div>
// Resolves to
<div th:utext="#{screen.home.welcome}"></div>
This would probably work as well:
<div th:utext="#{${#root.get(T(com.package.MyKeys).myKey)}}"></div>