8

I am trying to create a web application using Spring Boot and Thymeleaf and am having trouble getting the template to use the messages defined in a properties file. Instead of the message defined in the properties file, it is instead showing ??form.welcome_en_GB?? The console isn't logging any errors.

The project structure is like this

──┬  src
  │   └───  main
  │       ├───  java
  │       │   └───  com
  │       │       └───  package
  │       │           ├───  controller
  │       │           │   └─── FormController.java
  │       │           ├─── Application.java
  │       │           └─── ServletInitializer.java
  │       └───  resources
  │           ├───  static
  │           │   └─── home.html
  │           ├───  templates
  │           │   ├─── form.html
  │           │   └─── form.properties
  │           └─── application.properties
  └─── pom.xml

Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ServletInitializer.java

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

FormController.java

@Controller
@RequestMapping("/form")
public class FormController {
    private static final Logger log = LoggerFactory.getLogger(FormController.class);

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public ModelAndView getNewReportForm() {
        log.info("New form requested");
        ModelAndView mav = new ModelAndView("form");
        return mav;
    }
}

form.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" type="text/css" media="all"/>
</head>
<body>
<p th:text="#{form.welcome}">Welcome!</p>
</body>
</html>

form.properties

form.welcome=Hello there!
Mahozad
  • 18,032
  • 13
  • 118
  • 133
SelketDaly
  • 539
  • 1
  • 5
  • 20

2 Answers2

21

I believe that changing the name of form.properties to messages.properties and locating it in the root of your resources folder should allow spring boot to pick it up automagically.

When I have multiple message files I explicitly list them in a MessageSource bean so that the MVC auto-configuration picks them up, e.g.:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/some-mvc-messages", "classpath:/some-other-mvc-messages", "classpath:/another-projects/mvc-messages");
    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setCacheSeconds(5);
    return messageSource;
}
Dave Bower
  • 3,487
  • 26
  • 28
  • 1
    Thanks, I set the basename for the message source and its working now – SelketDaly Apr 11 '16 at 10:56
  • Thanks for the tip. I was running into issues where Thymeleaf internationalization message would not be resolved when running as a jar file but would run when debugging. Adding this Bean declaration made it resolve in both situations for me. – Jason Turan Sep 20 '18 at 14:00
  • Thank you! For some reason my `messages.properties` was in an `i18n` subdirectory of `src/main/resources` (maybe followed the advice of one of the many *terrible* blog posts about Spring Boot + Thymeleaf + i18n that tell you to do way too much manual config instead of letting Spring Boot do it for you). Moving it fixed this. – Frans Jun 05 '20 at 08:51
1

I Have the same Issue while using messages.properties file The issue was @EnableAutoConfiguration not added in my Spring boot file

 @SpringBootApplication
 @EnableAutoConfiguration
    public class Run {

        public static void main(String[] args) {
            SpringApplication.run(Run.class, args);
            System.out.println("Run");
        }

    }

After added its work

Ahmad
  • 41
  • 2