본문 바로가기

Spring 정리/Spring MVC 5

Properties / Message

반응형

변하지 않는 값을 설정할 때 Properties를 기억하라

 

Help - install New Software - Add에 들어가서 PropertyEditor를 설치한다. 

 

[WebContet] 하위폴더에 있는 [META-INF], [WEB-INF], [resources] 에 더해서 [properties]를 하나 생성한다.

 

그 안에 data1.properties, data2.properties를 생성한다.

 

1. Properties를 Controller에서 사용하기

<data1.properties>

aaa.a1 = 100
aaa.a2 = 문자열1

bbb.a2 = 200
bbb.c3 = 문자열2
<TestController.java>

@PropertySource("/WEB-INF/properties/data1.properties")
@PropertySource("/WEB-INF/properties/data2.properties")
				or
                
@PropertySources({
	@PropertySource("/WEB-INF/properties/data1.properties"),
	@PropertySource("/WEB-INF/properties/data2.properties")
})
public class TestController{
	@Value("${aaa.a1}")
    private int a1;
    
    @Value("${aaa.a2}")
    private String a2;
	...
}

@Value를 통해서 properties에 정의한 값을 주입할 수 있다.

properties 파일들을 삽입하고 싶으면 class 위에 정의해주어야 한다.

복수개의 파일이라면 @PropertiesSources안에 적어준다.

 

2. Properties를 jsp에서 사용하기

 

properties 파일을 message로 등록한다.

<ServletAppContext.java>

@Bean
	public ReloadableResourceBundleMessageSource messageSource() {
		ReloadableResourceBundleMessageSource res = new ReloadableResourceBundleMessageSource();

		// res.setBasename("/WEB-INF/properties/data1.properties");
		res.setBasenames("/WEB-INF/properties/data1.properties", "/WEB-INF/properties/data2.properties");

		return res;
	}

servlet을 설정하는 파일인 ServletAppContext에 Message등록을 한다.

@Bean을 주입하고, ReloadableResourceBundleMessageSource 의 인스턴스를 생성한다.

어떤 파일에 message를 적용시킬지 setBasenames를 통해 설정한다.

 

<TestController.java>

@Autowired
ReloadableResourceBundleMessageSource res;

@GetMapping("/test1")
	public String test1(Model model,Locale locale) {
		String a1 = res.getMessage("aaa.a1", null,null);
		String b1 = res.getMessage("bbb.b1", null,null);
		
		// {} 부분에 셋팅할 값 배열
		Object [] args = {30, "홍길동"};
		String a2 = res.getMessage("aaa.a2", args,null);
		
		String a3 = res.getMessage("aaa.a3",null, locale);
		
		System.out.println(a1);
		System.out.println(b1);
		System.out.println(a2);
		System.out.println(a3);
		
		model.addAttribute("args",args);
		return "test1";
	}
    
    ==========================================================
    <data1.properties>
    aaa.a2 = 나이는 {0}이고 이름은 {1}입니다.

등록된 Bean을 실제로 사용하기 위한 @Autowired를 주입한다.

getMessage를 사용해서 출력하고 싶은 내용을 정할 수 있다. 매개변수는 총 3개가 있다.

 

1. 사용하고자 하는 변수명

2. 안에셋팅할 값

3. 국가언어

 

data1에 정의한 aaa.a2라는 변수를 사용하며, args에 정의한 "30"과 "홍길동"을 각각 {0}, [1}에 넣으며,

locale은 한국이기 떄문에 자동으로 "ko"가 설정된다. 영어버전으로 설정하면, 영어로 표시되어 다국어 처리도 가능하다.

 

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

jsp에서 taglib에서 spring을 추가한다.

 

<test1.jsp>

<body>
	<h1>test1</h1>
	<h3>aaa.a1 : <spring:message code="aaa.a1"/></h3>
	<h3>bbb.b1 : <spring:message code="bbb.b1"/></h3>
	<h3>aaa.a2 : <spring:message code="aaa.a2" arguments="${args }"/></h3>
	<h3>aaa.a3 : <spring:message code="aaa.a3"/></h3>
</body>

spring을 통해 message를 처리할 수 있으며, Message에 담긴 args를 arguments에 출력한다.

또한 aaa.a3부분은 다국어로 처리된 부분으로, 언어설정에 맞게 변경된다.

 

반응형

'Spring 정리 > Spring MVC 5' 카테고리의 다른 글

에러 메시지 커스터마이징  (0) 2020.05.27
유효성 검사  (0) 2020.05.27
RequestScope 빈 주입  (0) 2020.05.26
RequestScope  (0) 2020.05.26
Redirect와 Forward  (0) 2020.05.26