-
message웹페이지 제작을 위한 스프링공부 2020. 5. 9. 23:29
message
properties message 등록
java
//properties를 message등록 @Bean public ReloadableResourceBundleMessageSource messageSource() { ReloadableResourceBundleMessageSource res = new ReloadableResourceBundleMessageSource(); //res.setBasename("/WEB-INF/properties/data1");//한개일때 res.setBasenames("/WEB-INF/properties/data1","/WEB-INF/properties/data2");//한개이상일때 return res; }
xml
<!-- Messagesource등록 --> <beans:bean class='org.springframework.context.support.ReloadableResourceBundleMessageSource' id='messageSource'> <!--properties가 한개일때 <beans:property name="basename" value=/WEB-INF/properties/data1"/> --> <beans:property name="basenames"> <beans:list> <beans:value>/WEB-INF/properties/data1</beans:value> <beans:value>/WEB-INF/properties/data2</beans:value> </beans:list> </beans:property> </beans:bean> <!-- messagesource를 사용하기 위한 messageaccessor등록 --> <beans:bean id='messageSourceAccessor' class='org.springframework.context.support.MessageSourceAccessor'> <beans:constructor-arg ref='messageSource'/> </beans:bean>
xml에서 주입후 출력
package kr.co.softcampus.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class TestController { @Autowired ReloadableResourceBundleMessageSource res; @GetMapping("/test1") public String test1() { String a1=res.getMessage("aaa.a1",null,null);//첫번째 입력한 데이터 두번째 {}{}값을 지정해줄때 세번째 다국어 String a2=res.getMessage("bbb.b1", null, null); System.out.println(a1); System.out.println(a2); return "test1"; } }
messagesource를 주입받고 getter을 사용하여 값을 불러옴
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>test1</h1> <h3>aaa.a1:<spring:message code='aaa.a1'/></h3> <h3>bbb.b1:<spring:message code='bbb.b1'/></h3> </body> </html>
test1에 출력
'웹페이지 제작을 위한 스프링공부' 카테고리의 다른 글
에러 메시지 커스터마이징 (0) 2020.05.12 유효성 검사 (0) 2020.05.10 properties (0) 2020.05.09 application scope (0) 2020.05.08 session scope (0) 2020.05.08