org.thymeleaf.exceptions.TemplateProcessingException: Message key for message resolution must be a non-null and non-empty String

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

org.thymeleaf.exceptions.TemplateProcessingException: Message key for message resolution must be a non-null and non-empty String

balteo
Hello,
I have the following html in a Thymeleaf template:
<div th:text="#{${member.eyeColor}}"></div>

The EyeColor enum is as follows:
public enum EyeColor {

	BLUE("kadjoukor.enum.eyeColor.blue"),
	GREEN("kadjoukor.enum.eyeColor.green"),
    BROWN("kadjoukor.enum.eyeColor.brown"),
    BLACK("kadjoukor.enum.eyeColor.black");
    
    private String messageKey;

	private EyeColor(String messageKey) {
		this.messageKey = messageKey;
	}

	public String getMessageKey() {
		return messageKey;
	}
    
    
}

I am not sure whether the above thymleaf code is OK in order to display the value of an enum.

As of now, I get the following exception:

org.thymeleaf.exceptions.TemplateProcessingException: Message key for message resolution must be a non-null and non-empty String

Can anyone please help?

Regards,

Julien.
Reply | Threaded
Open this post in threaded view
|

Re: org.thymeleaf.exceptions.TemplateProcessingException: Message key for message resolution must be a non-null and non-empty String

Emanuel
Administrator
So long as you've confirmed that member.eyeColor is not null, I think you need to supply the message key property to the th:utext, not just the enum:

th:utext="#{${member.eyeColor.messageKey}}"
Reply | Threaded
Open this post in threaded view
|

Re: org.thymeleaf.exceptions.TemplateProcessingException: Message key for message resolution must be a non-null and non-empty String

balteo
Hi Emanuel,
I am pretty sure the exception doesn't refer to the same "messageKey" variable as that in the enum.
My enum has a messageKey variable; the exception refers to a variable in a Thymeleaf framework class.
Do you see what I mean?
Regards,
J.
Reply | Threaded
Open this post in threaded view
|

Re: org.thymeleaf.exceptions.TemplateProcessingException: Message key for message resolution must be a non-null and non-empty String

Emanuel
Administrator
I just created a test case similar to what you've described in your original post, and was able to replicate the error message if I made the the member.eyeColor property resolve to null.  So please check if member.eyeColor has been set.

Then I played around with it and found that:
 - if member.eyeColor is not null, the page will display, but because the EyeColor object is not a string, it can't be turned into a message key to find a message property and I get something like ??TEST_en_NZ?? showing up on the page.
 - if member.eyeColor is not null and I change the th:utext to #{${member.eyeColor.messageKey}}, then it works and Thymeleaf is able to look up the messageKey value in the properties file.
Reply | Threaded
Open this post in threaded view
|

Re: org.thymeleaf.exceptions.TemplateProcessingException: Message key for message resolution must be a non-null and non-empty String

balteo
Hi Emanuel!
Emanuel wrote
 I change the th:utext to #{${member.eyeColor.messageKey}} then it works and Thymeleaf is able to look up the messageKey value in the properties file. ...
I noticed that too. I was actually looking for a way to encapsulate the "messageKey" variable and avoid using it in the view. I thought there would be a way to hide it from the view....