Spring MVC的國(guó)際化是建立在Java國(guó)際化的基礎(chǔ)上的眠寿,其一樣是通過(guò)提供不同國(guó)家的語(yǔ)言環(huán)境的消息資源怀各。通過(guò)ResourceBundle加載Locale對(duì)應(yīng)的資源文件。再取得該資源文件中指定Key對(duì)應(yīng)的消息。
步驟:
- 1.給系統(tǒng)加載國(guó)際化資源
- 2.輸出國(guó)際化戒祠。Spring MVC輸出國(guó)際化消息有兩種方式。
1.在頁(yè)面上輸出國(guó)際化消息速种。需要使用Spring MVC的標(biāo)簽庫(kù)姜盈。
2.在Controller的處理方法中輸出國(guó)際化消息。需要使用org.springframework.web.servlet.support.RequestContext的getMessage()方法來(lái)完成配阵。
1.Spring MVC國(guó)際化的相關(guān)知識(shí)
1.1 messageSource
利用messageSource bean告訴Spirng MVC國(guó)際化的屬性文件保存在哪里馏颂。
1.2 localeResolver
用戶(hù)選擇語(yǔ)言區(qū)域的時(shí)候,最常用的方法是通過(guò)讀取用戶(hù)瀏覽器的accept-language標(biāo)題值棋傍。其他方式還有讀取HttpSession或者Cookie救拉。
Spring MVC提供的包
- 1.AcceptHeaderLocaleResovler
讀取瀏覽器的accept-language標(biāo)題是默認(rèn)的,也是最容易使用的語(yǔ)言區(qū)域解析器瘫拣∫谛酰可以不用顯示配置。
2.SessionLocaleResovler
3.CookieLocaleResovler
上面兩個(gè)需要進(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ù)葱绒,可以是一個(gè)字符串感帅、數(shù)組或者對(duì)象
- 2.argumentSeparator 用來(lái)分割該標(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國(guó)際化
基于瀏覽器的讀取accept-language,來(lái)確認(rèn)語(yǔ)言區(qū)域帮毁,是默認(rèn)的方式实苞,通過(guò)AcceptHeaderLocaleResovler來(lái)處理。
因?yàn)槭悄J(rèn)實(shí)現(xiàn)方式烈疚,所以在Spring的xml配置里面黔牵,可以顯示配置或者不配置
- 新建資源文件
在resources文件下,新建language_en_US.properties
language.username=Username:
language.password=Password:
- 在xml里面配置加載國(guó)家化資源節(jié)點(diǎn)信息
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basename" value="language"/>
</bean>
- 配置mvc語(yǔ)言攔截器
因?yàn)锳cceptHeaderLocaleResovler是默認(rèn)的爷肝,所以xml無(wú)須配置
- JSP頁(yè)面代碼
<%@ 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)語(yǔ)言區(qū)域解析器猾浦,需要在Xml顯示配置。如果需要使用它灯抛,則Spring MVC會(huì)從HttpSession作用域獲取用戶(hù)所設(shè)置的語(yǔ)言區(qū)域金赦。
- 配置xml節(jié)點(diǎn)信息
<!--國(guó)際化語(yǔ)言攔截器-->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
<!--這邊一定要配置id并且名稱(chēng)為localeResolver-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
為什么上面配置id名稱(chēng)一定要為localeResolver呢。因?yàn)镈ispatcherServlet里面定義的默認(rèn)名稱(chēng)就是為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頁(yè)面
<%@ 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">中文</a>|<a href="/user/signupsession?request_locale=en_US">英文</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國(guó)際化
SessionLocaleResovler不是默認(rèn)語(yǔ)言區(qū)域解析器夹抗,需要在Xml顯示配置。如果需要使用它纵竖,則Spring MVC會(huì)從Cookie中獲取用戶(hù)所設(shè)置的語(yǔ)言區(qū)域漠烧。
- 配置xml節(jié)點(diǎn)
<!--國(guó)際化語(yǔ)言攔截器-->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"></bean>
- 后臺(tái)代碼
@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";
}
- 前臺(tái)Jsp頁(yè)面
<%@ 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國(guó)際化Cookie方式</title>
</head>
<body>
<a href="/user/signupcookie?request_locale=zh_CN">中文</a>|<a href="/user/signupcookie?request_locale=en_US">英文</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>