250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- ubuntu
- Python
- Core Java
- STS
- Tomcat
- Exception
- Eclipse
- 문서
- MSSQL
- SpringBoot
- Spring Boot
- PostgreSQL
- git
- AJAX
- jpa
- 설정
- oracle
- Thymeleaf
- JDBC
- JavaScript
- Open Source
- maven
- spring
- Docker
- Source
- myBatis
- MySQL
- error
- 오픈소스
- IntelliJ
Archives
- Today
- Total
헤르메스 LIFE
[Spring Boot] 개발환경 구축 - Messages 본문
728x90
개발환경
1. STS 버전 : 4.13.1
2. JDK 버전 : OpenJDK 11.0.14_9_x64
3. Tomcat 버전 : 9.0.71
4. Maven 버전 : 3.8.4
5. Spring 버전 : Spring Boot 2.7.8
6. Thymeleaf 3.0.15
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>simple.spring</groupId>
<artifactId>AppSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AppSpringBoot</name>
<description>Simple Spring Boot Project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
MessageConfig.java
package simple.spring.config;
import java.io.IOException;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@Configuration
public class MessageConfig {
@Bean
public MessageSource messageSource() throws IOException {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:/messages/messages", "classpath:/messages/label");
messageSource.setDefaultEncoding("UTF-8");
// locale에 해당하는 file이 없을 경우 system locale을 사용
messageSource.setFallbackToSystemLocale(false);
return messageSource;
}
@Bean
public MessageSourceAccessor messageSourceAccessor() throws IOException {
return new MessageSourceAccessor(messageSource());
}
}
ThymeleafController.java
package simple.spring.basic;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ThymeleafController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private MessageSourceAccessor messageSourceAccessor;
@GetMapping("/thymeleaf")
public String hello() {
return "thymeleaf";
}
@GetMapping("/message")
public String message() {
logger.info("en_US : " + messageSourceAccessor.getMessage("common.start_message", Locale.US));
logger.info("ko_KR : " + messageSourceAccessor.getMessage("common.start_message", Locale.KOREA));
return "message";
}
}
message.html
<!-- 네임 스페이스를 하나 추가 해 줍니다. -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>MessageSource Sample</title>
</head>
<body>
<h1 th:text="#{label.message}">메세지가 보여질 예정</h1>
</body>
</html>
Spring Messages 처리를 application.properties 파일에서 설정할수도 있습니다.
application.properties
#Server port
server.port=8081
server.servlet.context-path=/
#
server.servlet.encoding.enabled=true
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
# 기본단위 : 초
server.servlet.session.timeout=120
# Spring Default : UTF-8
server.tomcat.uri-encoding=UTF-8
#
spring.thymeleaf.cache=false
##############################################################
# Spring Message 처리
spring.messages.basename=messages/messages, messages/label
spring.messages.encoding=UTF-8
# locale에 해당하는 file이 없을 경우 system locale을 사용
# default : true
spring.messages.fallback-to-system-locale=false
# 해당 코드를 찾을 수 없을 경우 Code 를 출력한다.
# default : false
spring.messages.use-code-as-default-message=true
MessageConfig.java
package simple.spring.config;
import java.io.IOException;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor;
@Configuration
public class MessageConfig {
/**
* <pre>
* MessageSourceAccessor
* - 다양한 getMessage 메서드를 제공하는 MessageSource를 쉽게 접근하게 해주는 helper class
* </pre>
*
* @return
* @throws IOException
*/
@Bean
public MessageSourceAccessor messageSourceAccessor(MessageSource messageSource) {
return new MessageSourceAccessor(messageSource);
}
}
MessageSource의 Helper class인 MessageSourceAccessor 를 사용하기 위해서는 Configuration 셋팅이 추가되어야 합니다.
결론은 application.properties 에서 설정할 수도 있지만, 좀 더 디테일한 설정을 위해서는 application.properties 설정보다 Configuration 설정이 좋을 것 같습니다.
https://hermeslog.tistory.com/663
728x90
'Spring Boot Framework' 카테고리의 다른 글
[Spring Boot] Spring Boot에서 properties 값 주입받기 (0) | 2023.02.04 |
---|---|
[Spring Boot] 개발환경 구축 - File Upload (0) | 2023.02.04 |
[Spring Boot] Spring Boot 배포전략 (0) | 2023.01.31 |
[Spring Boot] 개발환경 구축 - Logback (0) | 2023.01.29 |
[Spring Boot] 개발환경 구축 - Hello Thymeleaf (0) | 2023.01.29 |