Hello World?

SPRING 에서 Message Properties 사용하는 방법 본문

JAVA WEB/SPRING

SPRING 에서 Message Properties 사용하는 방법

쮠이 2017. 7. 6. 14:27

SPRING 에서 공통화를 하다보면 메세지도 공통화를 시킬 필요가 있다.

아래는 i18n(국제화) 를 제외하고 프로퍼티 파일에서 메세지나 오브젝트 명들을 기술할때 사용하기 위해

프로퍼티를 사용하는 방법을 정리해 보았다

 

 message.properties 파일 작성

 ############################################
# JSP Message
############################################
msg.hello=hi my name is hong gil dong

 

 

root-context 파일에서 메세지 소스를 등록해주어야 한다

 

root-context-file 

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:p="http://www.springframework.org/schema/p"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:util="http://www.springframework.org/schema/util"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                                      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
                                      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                                      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
                                      http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
                                      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

 

   ...

  <!-- 메세지 소스 정의  -  위치는 classpath 의 시작위치를 말한다 -->

  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>message</value>
            </list>
        </property>
    </bean>

 

   ..

 

 

 

JSP나 JAVASCRIPT 에선 아래와 같은 방법으로 사용하면 된다.

 

 Example

 

   -- JAVASCRIPT 에서 사용하는 방법

   <script>

       alert ( '<spring:message code="admin.hello" /> ');

   </script>

 

   -- JSP에서 사용하는 방법

    <span> <spring:message code="admin.hello" /> </span>