context-message.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8" />
<property name="basename" value="classpath:gptwr3" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="ko" />
</bean>
classpath로 지정된 곳에
gptwr3_ko.properties
gptwr3_en.properties 파일이 존재
ex) gptwr3_ko.properties
errors.minlength={0}은 {1}자 이상 입력해야 합니다.
fail.common.msg=에러가 발생했습니다!
MessageUtils.java
import java.util.Locale;
import org.springramework.context.MessageSource;;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageUtils{
private static MessageSource resources = new ClassPathXmlApplicationContext("spring/context-message.xml");
public static String getMessage(String code){
return resources.getMessage(code, null, Locale.getDefault);
}
public static String getMessage(String code, String args[]){
return resources.getMessage(code, args, Locale.getDefault);
}
}
String msg2 = MessageUtils.getMessage("fail.common.msg");
System.out.println("msg2 : " + msg2); // msg: 에러가 발생했습니다!
<h3>3. JSP에서 메시지 사용하기</h3>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags">
<spring:message code="erros.minlength" arguments="사용자 이름, 2"></spring:message>
<spring:message code="fail.common.msg"></spring:message>