스프링에선 설정을 할 때 대표적인 파일형태로 properties 파일과 최근엔 yaml 파일을 사용한다. 오늘 학습할 properties 파일은 .properties 파일로, key(키)=value(값)
으로 설정 후 꺼내 사용할 수 있다.
↓ property01.properties 파일
com.main.title=사이트제목 com.main.var1=englishVar com.main.cnt1=350
위에서 정의한 properties 파일을 @PropertySource
어노테이션으로 불러올 수 있다.
@PropertySource
↓ Controller
@Controller
@PropertySource("/WEB-INF/properties/property01.properties")
public class MainController {
@Value("${com.main.title}")
String mainTitle;
@Value("${com.main.var1}")
String engVar;
@Value("${com.main.cnt1}")
int cnt1;
@RequestMapping("/main")
public String main(Model model) {
model.addAttribute("mainTitle", mainTitle);
model.addAttribute("engVar", engVar);
model.addAttribute("cnt1", cnt1);
System.out.println(mainTitle);
System.out.println(engVar);
System.out.println(cnt1);
return "main";
}
}
↓ 콘솔창 출력결과
↓ main.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>main</h1>
${mainTitle} <hr/>
${engVar} <hr/>
${cnt1} <hr/>
</body>
</html>
↓ main view 화면
@Autowired
이번엔 @Autowired
어노테이션으로 불러오겠다. @Autowired
는 스프링에 등록된 bean 객체를 자동으로 맵핑시켜주는 어노테이션이다.
↓ servlet-context.xml
<!-- Properties Message 처리 관련 설정 -->
<beans:bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
<beans:property name="basename" value="/WEB-INF/properties/property01"/>
</beans:bean>
<!-- 생성자 arg에 객체 넣기 -->
<beans:bean id="messageSourceAccessor" class="org.springframework.context.support.MessageSourceAccessor">
<beans:constructor-arg ref="messageSource"/>
</beans:bean>
↓ property01.properties
com.main.title=사이트제목 com.main.var1=englishVar com.main.cnt1=350 com.main.welcome=안녕하세요! 어서오세요! com.main.userWelcome={0}님 반갑습니다~! {1}사이트입니다~!
↓ Controller
@Controller
public class MsgController {
@Autowired
ReloadableResourceBundleMessageSource messageSource;
//servlet-context.xml 에서 설정한 id : messageSource
@RequestMapping("/msgPage")
public String msgPage(Model model) {
//getMessage("properties코드", Object[] args(파라미터값), Locale)
String m1 = messageSource.getMessage("com.main.title", null, null);
String m2 = messageSource.getMessage("com.main.var1", null, null);
String m3 = messageSource.getMessage("com.main.cnt1", null, null);
System.out.println(m1);
System.out.println(m2);
System.out.println(m3);
Object[] args = {"고객", "아주 좋은"};
String m4 = messageSource.getMessage("com.main.userWelcome", args, null);
Object[] args2 = {"손", "그저그런"};
String m5 = messageSource.getMessage("com.main.userWelcome", args2, null);
System.out.println(m4);
System.out.println(m5);
model.addAttribute("args", args);
model.addAttribute("args2", args2);
return "msgPage";
}
}
↓ msgPage.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
>>> 🔥 최상단에 추가
<%@ 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>msgPage</h1>
<p><spring:message code="com.main.title"/></p>
<p><spring:message code="com.main.var1"/></p>
<p><spring:message code="com.main.cnt1"/></p>
<p><spring:message code="com.main.welcome"/></p>
<p><spring:message code="com.main.userWelcome" arguments="${args}"/></p>
<p><spring:message code="com.main.userWelcome" arguments="${args2}"/></p>
</body>
</html>
↓ msgPage view 화면
↓ 콘솔창 출력결과
↓ property01_ko.properties
com.main.title=사이트제목 com.main.var1=englishVar com.main.cnt1=350 com.main.welcome=안녕하세요! 어서오세요! com.main.userWelcome={0}님 반갑습니다~! {1}사이트입니다~!
↓ property01_en.properties
com.main.title=TitleOfSite com.main.var1=englishVar com.main.cnt1=350 com.main.welcome=Hello! Welcome! com.main.userWelcome=Nice to meet {0}~! This is {1}~!
↓ Controller
@Controller
public class MsgController {
@Autowired
ReloadableResourceBundleMessageSource messageSource;
@RequestMapping("/msgPage")
public String msgPage(Model model, Locale locale) {
System.out.println(locale);
String msg_ko = messageSource.getMessage("com.main.welcome", null, locale);
String msg_en = messageSource.getMessage("com.main.welcome", null, Locale.ENGLISH);
System.out.println(msg_ko);
System.out.println(msg_en);
}
}
↓ 콘솔창 출력결과