歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring MVC國際化

Spring MVC國際化

日期:2017/3/1 10:21:12   编辑:Linux編程

本文基於Spring MVC 注解-讓Spring跑起來。本文提到的國際化是Spring實現國際化的方案之一。

(1) 在applicationContext.xml中添加以下配置信息:

  1. <!-- 國際化配置 -->
  2. <bean id="messageSource"
  3. class="org.springframework.context.support.ResourceBundleMessageSource">
  4. <property name="basename" value="messages.messages" />
  5. </bean>
上述代碼中提到的messages.messages中,前一個messages是src下的一個文件夾,後一個messages是所有以messages開頭的,以properties結尾的文件,如messages_zh_CN.properties或messages_en.properties,這些文件即是配置國際化信息的文件,其信息分別如下:
  1. #message_en.properties
  2. main.title=RUI manage system
  1. #message_zh_CN.properties
  2. main.title=RUI管理系統
(2) 在dispatcher.xml中添加以下配置信息
  1. <!-- 國際化配置 -->
  2. <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

並添加攔截器配置:
  1. <mvc:interceptors>
  2. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
  3. </mvc:interceptors>

(3) 在jsp頁面中調用國際化後的信息

  1. <fmt:message key="main.title" />

(4) 訪問系統,只需要在初次訪問系統時,在地址攔上添加"?locale=en"即可訪問英文網站。注意,訪問系統的第一個頁面時添加即可,後緒可不再添加。

Copyright © Linux教程網 All Rights Reserved