Spring MVC的國際化實(shí)現(xiàn)代碼
Spring MVC的國際化是建立在Java國際化的基礎(chǔ)上的,其一樣是通過提供不同國家的語言環(huán)境的消息資源。通過ResourceBundle加載Locale對應(yīng)的資源文件。再取得該資源文件中指定Key對應(yīng)的消息。
步驟:
1.給系統(tǒng)加載國際化資源
2.輸出國際化。Spring MVC輸出國際化消息有兩種方式。
- 在頁面上輸出國際化消息。需要使用Spring MVC的標(biāo)簽庫。
- 在Controller的處理方法中輸出國際化消息。需要使用org.springframework.web.servlet.support.RequestContext的getMessage()方法來完成。
1.Spring MVC國際化的相關(guān)知識
1.1 messageSource
利用messageSource bean告訴Spirng MVC國際化的屬性文件保存在哪里。
1.2 localeResolver
用戶選擇語言區(qū)域的時候,最常用的方法是通過讀取用戶瀏覽器的accept-language標(biāo)題值。其他方式還有讀取HttpSession或者Cookie。
Spring MVC提供的包
1.AcceptHeaderLocaleResovler
讀取瀏覽器的accept-language標(biāo)題是默認(rèn)的,也是最容易使用的語言區(qū)域解析器??梢圆挥蔑@示配置。
2.SessionLocaleResovler
3.CookieLocaleResovler
上面兩個需要進(jìn)行顯示配置。
1.3 message標(biāo)簽
Spring MVC中顯示本地化消息通常使用Spring的message標(biāo)簽。
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
message標(biāo)簽的屬性
1.arguments 標(biāo)簽的參數(shù),可以是一個字符串、數(shù)組或者對象
2.argumentSeparator 用來分割該標(biāo)簽參數(shù)的字符
3.code 獲取消息的key
4.text 如果code屬性不存在,所顯示的默認(rèn)文本
5.var 用于保存消息的變量
6.message MessageSourceResolvable參數(shù)
7.htmlEscape boolean值,表示被渲染的值是否應(yīng)該進(jìn)行HTML轉(zhuǎn)義
8.javaScriptEscape boolean值,表示被渲染的值是否應(yīng)該進(jìn)行javascript轉(zhuǎn)義
9.scope 保存var屬性中定義的變量的作用范圍
2.基于瀏覽器的accept-language國際化
基于瀏覽器的讀取accept-language,來確認(rèn)語言區(qū)域,是默認(rèn)的方式,通過AcceptHeaderLocaleResovler來處理。
因?yàn)槭悄J(rèn)實(shí)現(xiàn)方式,所以在Spring的xml配置里面,可以顯示配置或者不配置
新建資源文件
在resources文件下,新建language_en_US.properties
language.username=Username: language.password=Password:
在xml里面配置加載國家化資源節(jié)點(diǎn)信息
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource"> <property name="basename" value="language"/> </bean>
配置mvc語言攔截器
因?yàn)锳cceptHeaderLocaleResovler是默認(rèn)的,所以xml無須配置
JSP頁面代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> <html> <head> <title>Sign Up</title> </head> <body> <form:form method="post" action="regist" modelAttribute="user"> <table> <tr> <td><spring:message code="language.username"/></td> <td><form:input path="name"/></td> </tr> <tr> <td><spring:message code="language.password"/> </td> <td><form:password path="password"/></td> </tr> </table> </form:form> </body> </html>
3.SessionLocaleResovler
SessionLocaleResovler不是默認(rèn)語言區(qū)域解析器,需要在Xml顯示配置。如果需要使用它,則Spring MVC會從HttpSession作用域獲取用戶所設(shè)置的語言區(qū)域。
配置xml節(jié)點(diǎn)信息
<!--國際化語言攔截器--> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean> </mvc:interceptors> <!--這邊一定要配置id并且名稱為localeResolver--> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
為什么上面配置id名稱一定要為localeResolver呢。因?yàn)镈ispatcherServlet里面定義的默認(rèn)名稱就是為localeResolver。
后端代碼
@RequestMapping("signupsession") public String signupsession(String request_locale, Model model, HttpServletRequest request) { if (request_locale != null) { if (request_locale.equals("zh_CN")) request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh","CN")); else if (request_locale.equals("en_US")) request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en","US")); } User user = new User(); model.addAttribute("user", user); return "signup_session"; }
前端jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> <html> <head> <title>SessionLocaleResovler</title> </head> <body> <a href="/user/signupsession?request_locale=zh_CN" rel="external nofollow" >中文</a>|<a href="/user/signupsession?request_locale=en_US" rel="external nofollow" >英文</a> <form:form method="post" action="regist" modelAttribute="user"> <table> <tr> <td><spring:message code="language.username"/></td> <td><form:input path="name"/></td> </tr> <tr> <td><spring:message code="language.password"/></td> <td><form:password path="password"/></td> </tr> </table> </form:form> </body> </html>
4.CookieLocaleResovler國際化
SessionLocaleResovler不是默認(rèn)語言區(qū)域解析器,需要在Xml顯示配置。如果需要使用它,則Spring MVC會從Cookie中獲取用戶所設(shè)置的語言區(qū)域。
配置xml節(jié)點(diǎn)
<!--國際化語言攔截器--> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"></bean>
后臺代碼
@RequestMapping("signupcookie") public String signupcookie(String request_locale, Model model, HttpServletRequest request, HttpServletResponse response) { if (request_locale != null) { CookieLocaleResolver resolver = new CookieLocaleResolver(); if (request_locale.equals("zh_CN")) resolver.setLocale(request, response, new Locale("zh", "CN")); else if (request_locale.equals("en_US")) resolver.setLocale(request, response, new Locale("en", "US")); } User user = new User(); model.addAttribute("user", user); return "signup_cookie"; }
前臺Jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> <html> <head> <title>Spring國際化Cookie方式</title> </head> <body> <a href="/user/signupcookie?request_locale=zh_CN" rel="external nofollow" >中文</a>|<a href="/user/signupcookie?request_locale=en_US" rel="external nofollow" >英文</a> <form:form method="post" action="regist" modelAttribute="user"> <table> <tr> <td><spring:message code="language.username"/></td> <td><form:input path="name"/></td> </tr> <tr> <td><spring:message code="language.password"/></td> <td><form:password path="password"/></td> </tr> </table> </form:form> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Mybatis中result標(biāo)簽識別不了的情況
這篇文章主要介紹了解決Mybatis中result標(biāo)簽識別不了的情況,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01Java數(shù)據(jù)結(jié)構(gòu)之ArrayList從順序表到實(shí)現(xiàn)
Java中的ArrayList是一種基于數(shù)組實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu),支持動態(tài)擴(kuò)容和隨機(jī)訪問元素,可用于實(shí)現(xiàn)順序表等數(shù)據(jù)結(jié)構(gòu)。ArrayList在內(nèi)存中連續(xù)存儲元素,支持快速的隨機(jī)訪問和遍歷。通過學(xué)習(xí)ArrayList的實(shí)現(xiàn)原理和使用方法,可以更好地掌握J(rèn)ava中的數(shù)據(jù)結(jié)構(gòu)和算法2023-04-04淺談Spring-cloud 之 sleuth 服務(wù)鏈路跟蹤
本篇文章主要介紹了淺談Spring-cloud 之 sleuth 服務(wù)鏈路跟蹤,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Java中的ArrayList、LinkedList、HashSet等容器詳解
這篇文章主要介紹了Java中的ArrayList、LinkedList、HashSet等容器詳解,集合表示一組對象,稱為其元素,有些集合允許重復(fù)元素,而另一些則不允許,有些是有序的,有些是無序的,需要的朋友可以參考下2023-08-08使用java + selenium + OpenCV破解騰訊防水墻滑動驗(yàn)證碼功能
這篇文章主要介紹了使用java + selenium + OpenCV破解騰訊防水墻滑動驗(yàn)證碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Java多線程中線程池常見7個參數(shù)的詳解以及執(zhí)行流程
本文主要介紹了Java多線程中線程池常見7個參數(shù)的詳解以及執(zhí)行流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07