How to Read values from a properties file in thymleaf

praneeth :

Is there a way to read values from properties file in thymleaf using spring boot? eg:- validation messages, load labels from the properties file.

I'm using spring boot 2.1.3 version & thymleaf 3.0.11 version I need to dynamically set labels in my application using the properties file. I couldn't find a way to access properties file directly in thymleaf.

I tried creating lables.properties file in resources eg:-

field1 = "Name"
field2 = "Description"

I tried to access in thymleaf using like this,

<a href="./admin"><p class="text-uppercase" th:text="#{field1}"></p></a> 

But it was not working like this.

It displays as ??label1_en_US??" in my HTML view. Can anyone help me for sort this out?

mle :

Try to rename your lables.properties to messages.properties to get it resolved by Spring Boot automatically.

If this still is not working, consider defining an explicit message source like:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

The basenames are basically the name of your properties files with out the .properties extension.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=159283&siteId=1