Spring MVC的國際化實現(xiàn)代碼
Spring MVC的國際化是建立在Java國際化的基礎(chǔ)上的,其一樣是通過提供不同國家的語言環(huán)境的消息資源。通過ResourceBundle加載Locale對應的資源文件。再取得該資源文件中指定Key對應的消息。
步驟:
1.給系統(tǒng)加載國際化資源
2.輸出國際化。Spring MVC輸出國際化消息有兩種方式。
- 在頁面上輸出國際化消息。需要使用Spring MVC的標簽庫。
- 在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標題值。其他方式還有讀取HttpSession或者Cookie。
Spring MVC提供的包
1.AcceptHeaderLocaleResovler
讀取瀏覽器的accept-language標題是默認的,也是最容易使用的語言區(qū)域解析器。可以不用顯示配置。
2.SessionLocaleResovler
3.CookieLocaleResovler
上面兩個需要進行顯示配置。
1.3 message標簽
Spring MVC中顯示本地化消息通常使用Spring的message標簽。
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
message標簽的屬性
1.arguments 標簽的參數(shù),可以是一個字符串、數(shù)組或者對象
2.argumentSeparator 用來分割該標簽參數(shù)的字符
3.code 獲取消息的key
4.text 如果code屬性不存在,所顯示的默認文本
5.var 用于保存消息的變量
6.message MessageSourceResolvable參數(shù)
7.htmlEscape boolean值,表示被渲染的值是否應該進行HTML轉(zhuǎn)義
8.javaScriptEscape boolean值,表示被渲染的值是否應該進行javascript轉(zhuǎn)義
9.scope 保存var屬性中定義的變量的作用范圍
2.基于瀏覽器的accept-language國際化
基于瀏覽器的讀取accept-language,來確認語言區(qū)域,是默認的方式,通過AcceptHeaderLocaleResovler來處理。
因為是默認實現(xiàn)方式,所以在Spring的xml配置里面,可以顯示配置或者不配置
新建資源文件
在resources文件下,新建language_en_US.properties
language.username=Username: language.password=Password:
在xml里面配置加載國家化資源節(jié)點信息
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource"> <property name="basename" value="language"/> </bean>
配置mvc語言攔截器
因為AcceptHeaderLocaleResovler是默認的,所以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不是默認語言區(qū)域解析器,需要在Xml顯示配置。如果需要使用它,則Spring MVC會從HttpSession作用域獲取用戶所設置的語言區(qū)域。
配置xml節(jié)點信息
<!--國際化語言攔截器--> <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呢。因為DispatcherServlet里面定義的默認名稱就是為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不是默認語言區(qū)域解析器,需要在Xml顯示配置。如果需要使用它,則Spring MVC會從Cookie中獲取用戶所設置的語言區(qū)域。
配置xml節(jié)點
<!--國際化語言攔截器--> <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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實踐
h2是內(nèi)存數(shù)據(jù)庫,查詢高效,可以在開發(fā)初期使用它。本文主要介紹了SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實踐,具有一定的參考價值,感興趣的可以了解一下2021-09-09
Java開發(fā)環(huán)境配置教程(win7 64bit)
這篇文章主要為大家詳細介紹了win7 64bit下Java開發(fā)環(huán)境的配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
java.imageIo給圖片添加水印的實現(xiàn)代碼
最近項目在做一個商城項目, 項目上的圖片要添加水?、?添加圖片水印;②:添加文字水印;一下提供下個方法,希望大家可以用得著2013-07-07
Mybatis-plus如何查詢表中指定字段(不查詢?nèi)孔侄?
這篇文章主要介紹了Mybatis-plus如何查詢表中指定字段(不查詢?nèi)孔侄?,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

