티스토리 뷰

   스프링에서 제공하는 ReloadableResourceBundleMessageSource를 이용해서 공통 메시지를 처리해보도록 한다.

   

   찾아보니까 깔끔하게 정리된 게 없어서 내가 정리한다 ㅡ.ㅡ



   1. ReloadableResourceBundleMessageSource를 사용하기 위한 설정


context-message.xml


	
	
		
			classpath:/egovframework/message/message
		
	


	


message 폴더 안에 message_en_US.properties, message_ok_KR.properties 두 개의 파일이 있다면, 접속하는 세션의 로케일에 따라 자동으로 로딩한다.




message_ok_KR.properties

errors.minlength={0} 은 {1}자 이상 입력해야 합니다.
fail.common.msg=에러가 발생했습니다!




MessageUtils.java

import java.util.Locale;

import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MessageUtils {

	private static MessageSource resources = new ClassPathXmlApplicationContext("egovframework/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);
	}

}



   2. Java에서 메시지 사용하기


String[] args = {"사용자 이름", "2"};
String msg = MessageUtils.getMessage("errors.minlength", args);
System.out.println("msg : " + msg); // "msg : 사용자 이름 은 2자 이상 입력해야 합니다."

String msg2 = MessageUtils.getMessage("fail.common.msg");
System.out.println("msg2 : " + msg2); // "msg : 에러가 발생했습니다!"



   3. JSP에서 메시지 사용하기


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


 




댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함