- 内容提要
- 作者简介
- 译者简介
- 前言
- HTTP
- Servlet 和 JSP
- 下载 Spring 或使用 STS 与 Maven/Gradle
- 手动下载 Spring
- 使用 STS 和 Maven/Gradle
- 下载 Spring 源码
- 本书内容简介
- 下载示例应用
- 第 1 章Spring 框架
- 第 2 章模型 2 和 MVC 模式
- 第 3 章Spring MVC 介绍
- 第 4 章基于注解的控制器
- 第 5 章数据绑定和表单标签库
- 第 6 章转换器和格式化
- 第 7 章验证器
- 第 8 章表达式语言
- 第 9 章JSTL
- 第 10 章国际化
- 第 11 章上传文件
- 第 12 章下载文件
- 第 13 章应用测试
- 附录 A Tomcat
- 附录 B Spring Tool Suite 和 Maven
- 附录 C Servlet
- 附录 D JavaServer Pages
- 附录 E 部署描述符
10.5 范例
举例来说,i18n 应用程序展示了用 localeResolver bean 将 JSP 页面中的消息本地化的方法。其目录结构如图 10.1 所示,i18n 的 Spring MVC 配置文件如清单 10.1 所示。
图 10.1 i18n 的目录结构
清单 10.1 i18n 的 Spring MVC 配置文件
< ?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/springcontext.xsd">
< context:component-scan base-package="controller" />
< mvc:annotation-driven/>
< mvc:resources mapping="/css/ **" location="/css/" />
< mvc:resources mapping="/ *.html" location="/" />
< bean id="viewResolver" class="org.springframework.web.servlet.view.
→ InternalResourceViewResolver">
< property name="prefix" value="/WEB-INF/jsp/" />
< property name="suffix" value=".jsp" />
< /bean>
< bean id="messageSource" class="org.springframework.context.support.
→ ReloadableResourceBundleMessageSource">
< property name="basenames" >
< list>
< value>/WEB-INF/resource/messages< /value>
< value>/WEB-INF/resource/labels< /value>
< /list>
< /property>
< /bean>
< bean id="localeResolver" class="org.springframework.web.servlet.i18n.
→ AcceptHeaderLocaleResolver">
< /bean>
< /beans>
这里用到了 messageSource bean 和 localeResolver bean 这两个 bean。messageSource bean 声明用两个基准名设置了 basenames 属性:/WEB-INF/resource/messages 和/WEB-INF/resource/labels。localeResolver bean 利用 AcceptHeaderLocaleResolver 类实现消息的本地化。
它支持 en 和 fr 两个语言区域,因此每个属性文件都有两种版本。为了实现本地化,JSP 页面中的每一段文本都要用 message 标签代替。清单 10.2 展示了 ProductForm.jsp 页面。注意,为了达到调试的目的,当前的语言区域和 accept-language 标题显示在页面的最前面。
清单 10.2 ProductForm.jsp 页面
<%@ taglib prefix="form"
uri="http://www.springframework.org/tags/form"%>
<%@ taglib
prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML>
<html>
<head>
<title><spring:message code="page.productform.title"/></title>
<style type="text/css">@import url("<c:url
value="/css/main.css"/>");</style>
</head>
<body>
<div id="global">
Current Locale : ${pageContext.response.locale}
<br/>
accept-language header: ${header["accept-language"]}
<form:form commandName="product" action="product_save"
method="post">
<fieldset>
<legend><spring:message code="form.name"/></legend>
<p>
<label for="name"><spring:message
code="label.productName" text="default text" />:
</label>
<form:input id="name" path="name"
cssErrorClass="error"/>
<form:errors path="name" cssClass="error"/>
</p>
<p>
<label for="description"><spring:message
code="label.description "/>
</label>
<form:input id="description" path="description"/>
</p>
<p>
<label for="price"><spring:message code="label.price"
text="default text" />: </label>
<form:input id="price" path="price"
cssErrorClass="error"/>
<form:errors path="price" cssClass="error"/>
</p>
<p id="buttons">
<input id="reset" type="reset"
value="<spring:message code="button.reset"/>">
<input id="submit" type="submit"
value="<spring:message code="button.submit"/>">
</p>
</fieldset>
</form:form>
</div>
</body>
</html>
为了测试 i18n 的国际化特性,要修改浏览器的 accept-language 标签。
对于 Chrome 浏览器,打开“设置”页面,点击“显示高级设置”,点击“语言和输入设置”,添加并移动语言到列表的顶部。
对于 IE 浏览器,到 Tools >Internet Options > General (tab) > Languages > Language Preference 中修改。在 Language Preference 窗口中,单击 Add 按钮添加一种语言。当选择了多种语言时,为了修改某一种语言的优先值,要使用 Move up 和 Move down 按钮。
在其他浏览器中修改 accept-language 标题的说明,请访问以下网址查阅:
http://www.w3.org/International/questions/qa-lang-priorities.en.php
如果要对这个应用程序进行测试,访问以下 URL:
http://localhost:8080/i18n/add-product
你将会看到 Product 表的英语版和法语版,分别如图 10.2 和图 10.3 所示。
图 10.2 语言区域为 en_US 的 Product 表
图 10.3 语言区域为 fr_CA 的 Product 表
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论