본문 바로가기

Development/Spring

Message Bundle 사용하기

다국어를 사용하기 위해서
모든 메시지를 message 프로퍼티 파일에 모아둔다.

main-servlet.xml 에서

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="cacheSeconds" value="10"/>
    <property name="basenames">
        <list>
            <value>/WEB-INF/messages/messages</value>
        </list>
    </property>
</bean>

위의 예는 모든 messages 파일들을 WEB-INF 디렉토리 밑의 messages 디렉토리에서 관리하고 있다는 뜻이다.
ReloadableResourceBundleMessageSource 은 messages 파일이 수정될때 WAS를 재가동 하지 않고도 reloading 할수 있도록 해준다.
인터벌은 cacheSeconds에서 설정한다.


<%@ page contentType="text/html; charset=UTF-8" %>
<html>
  <head>
    <meta http-equiv="Refresh" content= "0; URL=index.do"/>
    <title>Start Web Application</title>
  </head>
  <body>
    <p>Please wait for the web application to start.</p>
  </body>
</html>

index 페이지에서 바로 index.do를 타도록 설정하고,

public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
    System.out.println("Index페이지로 이동합니다.");
    return new ModelAndView("index");
}

컨트롤러에서 받아서 처리한다.
주의할 점은, 주소창에서 직접 호출하면 spring 에서 messages를 인식할 수가 없다. 반드시 controller를 통해서 index.jsp -> index.do -> /WEB-INF/views/index.jsp 의 순서로 호출해야 한다.