[JAVA] Going out of message (Spring boot)

Step 1 ~ Setting applicaton.properties ~

Write as follows in src / main / resources / application.properties. If the locale is ja in Japanese environment, read messages_ja.properties.

application.properties


spring.messages.basename=messages  //File name (no extension required)
spring.messages.cache-duration=-1
spring.messages.encoding=UTF-8 

Step 2 ~ Messages.properties setting (message file) ~

Create src / main / resources / messages.properties

If there is no file ** messages.properties **, auto-configuration of messageSource will not be executed and an error message will be displayed at startup, so create an empty file.

Multilingual

When multilingualizing, prepare a file after messages that indicates the locale, for example, messages_en.properties for English (en) and messages_ja.properties for Japanese. This time, create the locale messages_ja.properties which means Japanese.

messages_ja.properties



#Hello World
hello.world=Hello World

Step 3 ~ Reference on Java side ~

Define MessageSource as a property.

UserController.java



@Controller
public class UserController {

@Autowired
protected MessageSource messageSource;

@RequestMapping("/")
public String hello(Model model) {

     model.addAttribute("Msg", messageSource.getMessage("hello.world", null, Locale.JAPAN));
     return "hello";
   }

}

Step 4 ~ Reference with HTML + thymeleaf ~

hello.html



<h1 th:text="${Msg}">XXXXX</h1>

Others ~ Refer directly to HTML + tymeleaf ~

hello.html



<h1 th:text="#{hello.world}">XXXXX</h1>

Recommended Posts

Going out of message (Spring boot)
Spring Boot validation message changes
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
[Spring Boot] Role of each class
Message cooperation started with Spring Boot
I want to control the default error message of Spring Boot
Summary of going to JJUG CCC 2019 Spring
WebMvcConfigurer Memorandum of Understanding for Spring Boot 2.0 (Spring 5)
How to split Spring Boot message file
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Summary of what I learned about Spring Boot
The story of raising Spring Boot 1.5 series to 2.1 series
Let's check the feel of Spring Boot + Swagger 2.0
Various correspondence table of Spring Framework and Spring Boot
When @Transactional of Spring Boot does not work
[Java / Spring Boot] Spring security ④ --Implementation of login process
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Verification] Comparison of Spring Boot vs Micronaut boot speed
[Spring] Pitfalls of BeanUtils.copyProperties
Specify the encoding of static resources in Spring Boot
Message cooperation started with Spring Boot Apache Kafka edition
I checked asynchronous execution of queries in Spring Boot 1.5.9
Spring boot memo writing (1)
A memorandum of addiction to Spring Boot2 x Doma2
05. I tried to stub the source of Spring Boot
Asynchronization of message transmission
About DI of Spring ①
First Spring Boot (DI)
SPRING BOOT learning record 02
I tried to reduce the capacity of Spring Boot
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
About DI of Spring ②
Learning Spring Boot [Beginning]
How to use CommandLineRunner in Spring Batch of Spring Boot
Spring boot memo writing (2)
Create Restapi with Spring Boot ((1) Until Run of App)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
How to boot by environment with Spring Boot of Maven
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Specify spring.profiles.active via context-param of web.xml in Spring Boot
Overview of Spring AOP
Download with Spring Boot
Let's find out how to receive in Request Body with REST API of Spring Boot
The story of raising Spring Boot from 1.5 series to 2.1 series part2
About the function of Spring Boot due to different versions
Minimal customization of Spring Boot error page (implementation of ErrorController interface)
Accelerate testing of Validators that require DI in Spring Boot
A story packed with the basics of Spring Boot (solved)
Extract SQL to property file with jdbcTemplate of spring boot
Execution of initial processing using Spring Boot Command Line Runner
Introductory hands-on for beginners of Spring 5 & Spring Boot 2 has been released
Impressions of an engineer apprentice going to JJUG CCC 2018 Spring