2017년 6월 22일 목요일

spring 다국어 설정

1. context-common.xml 작성

<?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:jdbc="http://www.springframework.org/schema/jdbc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
 <!-- CookieLocaleResolver : 쿠키를 이용한 locale정보 사용 
      SessionLocaleResolver : 세션을 이용한 locale정보 사용 
      AcceptHeaderLocaleResolver : 클라이언트의 브라우져에 설정된 locale정보 사용 
       Bean 설정 파일에 정의하지 않을 경우 AcceptHeaderLocaleResolver 를 default 로 적용된다. -->
 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
 <!-- 사용자의 브라우져의 Locale 정보를 이용하지 않고 사용자가 선택하여 언어를 직접 선택할 수 있도록 구현하려 한다면 
      CookieLocaleResolver 나 SessionLocaleResolver 를 이용한다.
                     먼저 다국어를 지원해야 하므로 메세지를 MessageSource 로 추출하여 구현해야 한다. -->
 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basenames">
   <list>
    <value>classpath:/egovframework/message/message-common</value> <!-- 파일경로 -->
   </list>
  </property>
  <property name="cacheSeconds">
   <value>60</value>
  </property>
 </bean>
    
</beans>

2. dispatcher-servlet.xml 수정

<mvc:interceptors>  
 <mvc:interceptor>
  <mvc:mapping path="/**" />
  <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
   <property name="paramName" value="lang" />
  </bean>
 </mvc:interceptor>
</mvc:interceptors>

3. Web 을 통해 들어오는 요청을 Charset UTF-8 적용한다. CharacterEncodingFilter 을 이용하여 encoding 할 수 이도록 아래와 같이 세팅한다


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>dev</display-name>
    
 <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>utf-8</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
    
 <filter>
  <filter-name>HTMLTagFilter</filter-name>
  <filter-class>egovframework.rte.ptl.mvc.filter.HTMLTagFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>HTMLTagFilter</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>
    
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:egovframework/spring/context-*.xml</param-value>
 </context-param>
    
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
    
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/config/springmvc/dispatcher-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
    
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
    
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
    
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
    
 <error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/common/error.jsp</location>
 </error-page>
 <error-page>
  <error-code>404</error-code>
  <location>/common/error.jsp</location>
 </error-page>
 <error-page>
  <error-code>500</error-code>
  <location>/common/error.jsp</location>
 </error-page>
    
</web-app>

4. properties 파일 추가 


각 파일에 key value 값으로 작성한다
ex) button.search=Search

5. jsp에서 spring태그를 이용해서 사용한다.


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

6. 다국어 설정 되었는지 테스트 

ex) http://localhost:8080/list.do?lang=ko / http://localhost:8080/list.do?lang=en
Share:

0 개의 댓글:

댓글 쓰기