當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring MVC国际化
生活随笔
收集整理的這篇文章主要介紹了
Spring MVC国际化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Spring MVC國際化簡介 ? ? ? ?程序國際化是商業系統的一個基本要求,因為今天的軟件系統不再是簡單的單機程序,往往都是一個開放的系統,需要面對來自全世界各個地方的訪問者,因此,國際化成為商業系統必不可少的一部分。 ? ? ? ?Spring MVC的國際化是建立在Java國際化的基礎之上的,其一樣也是通過提供不同國家/語言環境的消息資源,然后通過 Resource Bundle加載指定 Locale對應的資源文件,再取得該資源文件中指定key對應的消息。這整個過程與Java程序的國際化完全相同,只是 Spring MVC框架對Java程序國際化進行了進一步的封裝,從而簡化了應用程序的國際化。 二、Spring MVC國際化的知識: ? ? ?1、messageSource接口:告訴系統國際資源文件的存儲位置。? ? ? ? ? ? ? ? ? ? ? ? ?org.springframework.context.support.ResourceBundleMessageSource類 ? ? ?2、LocaleResolver接口:確定語言區域 ? ? ? ? ? (1)accept-langage:基于瀏覽器的語言區域選擇? ---? 默認方式,不需要配置 ? ? ? ? ? (2)SessionLocaleResolver:基于會話的語言區域選擇,需要配置(常用) ? ? ? ? ? (3)CookieLocaleResolver:基于Cookie的語言區域選擇,需要配置 ? ?? ? ? ?3、LocaleChangeInterceptor攔截器:國際化的攔截器,當語言區域發生改變時,該攔截器將進行攔截,根據傳遞的參數來改變應用的語言環境。需要在SpringMVC的配置文件中進行注冊 ? ? ? 4、message標簽:是Spring MVC的標簽,在視圖頁面中輸出國際化的消息 ? ? ? ? ?<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 三、Spring MVC國際化步驟: ? ?(1)給系統加載國際化資源文件。 ? ?(2)輸出國際化。 Spring MVC輸出國際化消息有兩種方式: ? ? ? ? ?A、在視圖頁面上輸出國際化消息,需要使用 Spring MVC的標簽庫。 ? ? ? ? ?B、在 Controller的處理方法中輸出國際化消息,需要使用 org.springframework.web.servlet.support Requestcontext的getMessage()方法來完成。 四、基于SessionLocaleResolver的國際化: 1、創建資源文件 ? ? ? ? ?messages_zh_CN.properties ? ? ? ? ?messages_en_US.properties ? ? ? 2、配置國際化的類和攔截器 ?<!-- 基于SessionLocaleResolver的語言區域選擇器 --> ? ? ? ?<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> ? ? ? ?<!-- 注冊MessageSource,明確資源文件的位置 --> ? ? ? ?<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> ? ? ? ? ??<property name="basename" value="messages"></property> ? ? ? ?</bean> ? ? ? ? ?<!-- 配置攔截器 --> ? ? ? ?<mvc:interceptors> ? ? ? ?<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> ? ? ? ? ??<property name="paramName" value="lang"></property> ? ? ? ?</bean> ? ? ??</mvc:interceptors> ? ? ? 3、在頁面中使用message標簽輸出國際化信息 ? ???<spring:message code="language"/>: ????????? <a href="?lang=zh_CN"> ??????????? <spring:message code="language.cn"/> ????????? </a>? ------ ????????? <a href="?lang=en_US"> ??????????? <spring:message code="language.en"/> ????????? </a> ????????? <br><br> ????????? <div align="center"> ???????????? <h2> ??????????????? <spring:message code="userlogin"/> ???????????? </h2> ???????????? <hr><br> ????????????? <spring:message code="username"/>: ???????????? <input type="text"> ???????????? <br><br> ????????????? <spring:message code="password"/> ???????????? <input type="password"> ???????????? <br><br> ???????????? <input type="submit" value="<spring:message code="submit"/>"> ???????????? ???????????? <input type="reset" value="<spring:message code="reset"/>"> ????????? </div> ? ? ? ?
轉載于:https://www.cnblogs.com/lone5wolf/p/10943869.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Spring MVC国际化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: day04_列表
- 下一篇: Py中的heapq模块【转载】