當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring MVC国际化
生活随笔
收集整理的這篇文章主要介紹了
Spring MVC国际化
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、Spring MVC國(guó)際化簡(jiǎn)介 ? ? ? ?程序國(guó)際化是商業(yè)系統(tǒng)的一個(gè)基本要求,因?yàn)榻裉斓能浖到y(tǒng)不再是簡(jiǎn)單的單機(jī)程序,往往都是一個(gè)開(kāi)放的系統(tǒng),需要面對(duì)來(lái)自全世界各個(gè)地方的訪問(wèn)者,因此,國(guó)際化成為商業(yè)系統(tǒng)必不可少的一部分。 ? ? ? ?Spring MVC的國(guó)際化是建立在Java國(guó)際化的基礎(chǔ)之上的,其一樣也是通過(guò)提供不同國(guó)家/語(yǔ)言環(huán)境的消息資源,然后通過(guò) Resource Bundle加載指定 Locale對(duì)應(yīng)的資源文件,再取得該資源文件中指定key對(duì)應(yīng)的消息。這整個(gè)過(guò)程與Java程序的國(guó)際化完全相同,只是 Spring MVC框架對(duì)Java程序國(guó)際化進(jìn)行了進(jìn)一步的封裝,從而簡(jiǎn)化了應(yīng)用程序的國(guó)際化。 二、Spring MVC國(guó)際化的知識(shí): ? ? ?1、messageSource接口:告訴系統(tǒng)國(guó)際資源文件的存儲(chǔ)位置。? ? ? ? ? ? ? ? ? ? ? ? ?org.springframework.context.support.ResourceBundleMessageSource類 ? ? ?2、LocaleResolver接口:確定語(yǔ)言區(qū)域 ? ? ? ? ? (1)accept-langage:基于瀏覽器的語(yǔ)言區(qū)域選擇? ---? 默認(rèn)方式,不需要配置 ? ? ? ? ? (2)SessionLocaleResolver:基于會(huì)話的語(yǔ)言區(qū)域選擇,需要配置(常用) ? ? ? ? ? (3)CookieLocaleResolver:基于Cookie的語(yǔ)言區(qū)域選擇,需要配置 ? ?? ? ? ?3、LocaleChangeInterceptor攔截器:國(guó)際化的攔截器,當(dāng)語(yǔ)言區(qū)域發(fā)生改變時(shí),該攔截器將進(jìn)行攔截,根據(jù)傳遞的參數(shù)來(lái)改變應(yīng)用的語(yǔ)言環(huán)境。需要在SpringMVC的配置文件中進(jìn)行注冊(cè) ? ? ? 4、message標(biāo)簽:是Spring MVC的標(biāo)簽,在視圖頁(yè)面中輸出國(guó)際化的消息 ? ? ? ? ?<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 三、Spring MVC國(guó)際化步驟: ? ?(1)給系統(tǒng)加載國(guó)際化資源文件。 ? ?(2)輸出國(guó)際化。 Spring MVC輸出國(guó)際化消息有兩種方式: ? ? ? ? ?A、在視圖頁(yè)面上輸出國(guó)際化消息,需要使用 Spring MVC的標(biāo)簽庫(kù)。 ? ? ? ? ?B、在 Controller的處理方法中輸出國(guó)際化消息,需要使用 org.springframework.web.servlet.support Requestcontext的getMessage()方法來(lái)完成。 四、基于SessionLocaleResolver的國(guó)際化: 1、創(chuàng)建資源文件 ? ? ? ? ?messages_zh_CN.properties ? ? ? ? ?messages_en_US.properties ? ? ? 2、配置國(guó)際化的類和攔截器 ?<!-- 基于SessionLocaleResolver的語(yǔ)言區(qū)域選擇器 --> ? ? ? ?<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> ? ? ? ?<!-- 注冊(cè)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、在頁(yè)面中使用message標(biāo)簽輸出國(guó)際化信息 ? ???<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> ? ? ? ?
轉(zhuǎn)載于:https://www.cnblogs.com/lone5wolf/p/10943869.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Spring MVC国际化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: day04_列表
- 下一篇: Py中的heapq模块【转载】