一:说明
1.说明
在页面上可以根据浏览器语言设置的情况对文本,时间,数值进行本地化处理
在bean中获取国际化资源文件Locale对应的信息
可以通过超链接切换Locale,而不再依赖于浏览器的语言设置情况
2.介绍
二:普通的国家化
1.在i18n.properties中写字段
2.i18n_zh_CN.properties
3.i18n_en_US.properties
4.index.jsp
可以跳转到i18n.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script> 8 <script type="text/javascript"> 9 $(function(){ 10 $("#testJson").click(function(){ 11 var url=this.href; 12 var args={}; 13 $.post(url,args,function(data){ 14 for(var i=0;i<data.length;i++){ 15 var id=data[i].id; 16 var lastName=data[i].lastName; 17 alert(id+":"+lastName); 18 } 19 }); 20 return false; 21 }); 22 }) 23 </script> 24 <title>Insert title here</title> 25 </head> 26 <body> 27 <br> 28 <a href="emps">list emps</a> 29 <br><br> 30 <a href="testJson" id="testJson">Test Json</a> 31 <br><br> 32 <!-- <form action="testHttpMessageConverter" method="post" enctype="multipart/form-data"> 33 File:<input type="file" name="file"/> 34 Desc:<input type="text" name="desc"/> 35 <input type="submit" value=Submit""> 36 </form> --> 37 <a href="testResponseEntity">Test ResponseEntity</a> 38 39 <br><br> 40 <a href="i18n">I18N Page</a> 41 42 </body> 43 </html>
5.在view下写一个i18n.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <fmt:message key="i18n.user"></fmt:message> 12 <br><br> 13 <a href="i18n2">I18N2 Page</a> 14 15 </body> 16 </html>
6.然后是i18n2.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <fmt:message key="i18n.password"></fmt:message> 12 <br><br> 13 <a href="i18n">I18N2 Page</a> 14 15 </body> 16 </html>
7.静态资源的访问
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 12 <!-- 配置自定义扫描的包 --> 13 <context:component-scan base-package="com.spring.it" ></context:component-scan> 14 15 <!-- 配置视图解析器 --> 16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="prefix" value="/WEB-INF/views/" /> 18 <property name="suffix" value=".jsp" /> 19 </bean> 20 21 <mvc:annotation-driven></mvc:annotation-driven> 22 23 <!-- 转换器 --> 24 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 25 <property name="converters"> 26 <set> 27 <ref bean="employeeConverter"/> 28 </set> 29 </property> 30 </bean> 31 <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> 32 33 <mvc:default-servlet-handler/> 34 <mvc:annotation-driven ignore-default-model-on-redirect="true"></mvc:annotation-driven> 35 36 <!-- 国家化 --> 37 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 38 <property name="basename" value="i18n"></property> 39 </bean> 40 41 <mvc:view-controller path="/i18n" view-name="i18n"/> 42 <mvc:view-controller path="/i18n2" view-name="i18n2"/> 43 </beans>
三:让连接走后台
1.注释掉i18n静态资源
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 12 <!-- 配置自定义扫描的包 --> 13 <context:component-scan base-package="com.spring.it" ></context:component-scan> 14 15 <!-- 配置视图解析器 --> 16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="prefix" value="/WEB-INF/views/" /> 18 <property name="suffix" value=".jsp" /> 19 </bean> 20 21 <mvc:annotation-driven></mvc:annotation-driven> 22 23 <!-- 转换器 --> 24 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 25 <property name="converters"> 26 <set> 27 <ref bean="employeeConverter"/> 28 </set> 29 </property> 30 </bean> 31 <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> 32 33 <mvc:default-servlet-handler/> 34 <mvc:annotation-driven ignore-default-model-on-redirect="true"></mvc:annotation-driven> 35 36 <!-- 国家化 --> 37 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 38 <property name="basename" value="i18n"></property> 39 </bean> 40 41 <!-- <mvc:view-controller path="/i18n" view-name="i18n"/> --> 42 <mvc:view-controller path="/i18n2" view-name="i18n2"/> 43 </beans>
2.控制器
1 @Autowired(required=true) 2 private ResourceBundleMessageSource message; 3 4 @RequestMapping("/i18n") 5 public String testi18n(Locale locale) { 6 String val=message.getMessage("i18n.user", null, locale); 7 return "i18n"; 8 }
四:通过超链接切换Locale
1.原理
2.配置xml
配置SessionLocaleResolver
配置LocaleChangeInterceter拦截器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 12 <!-- 配置自定义扫描的包 --> 13 <context:component-scan base-package="com.spring.it" ></context:component-scan> 14 15 <!-- 配置视图解析器 --> 16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="prefix" value="/WEB-INF/views/" /> 18 <property name="suffix" value=".jsp" /> 19 </bean> 20 21 <mvc:annotation-driven></mvc:annotation-driven> 22 23 <!-- 转换器 --> 24 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 25 <property name="converters"> 26 <set> 27 <ref bean="employeeConverter"/> 28 </set> 29 </property> 30 </bean> 31 <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> 32 33 <mvc:default-servlet-handler/> 34 <mvc:annotation-driven ignore-default-model-on-redirect="true"></mvc:annotation-driven> 35 36 <!-- 国家化 --> 37 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 38 <property name="basename" value="i18n"></property> 39 </bean> 40 41 <!-- <mvc:view-controller path="/i18n" view-name="i18n"/> --> 42 <mvc:view-controller path="/i18n2" view-name="i18n2"/> 43 44 <!-- 配置SessionLocaleResolver --> 45 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean> 46 47 <!-- 配置LocaleChangeInterceter拦截器 --> 48 <mvc:interceptors> 49 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean> 50 </mvc:interceptors> 51 52 </beans>
3.多写两个链接
主要是中文与英文的连接
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <fmt:message key="i18n.user"></fmt:message> 12 <br><br> 13 <a href="i18n2">I18N2 Page</a> 14 15 <!-- 多加两个链接,验证链接的切换 --> 16 <br><br> 17 <a href="i18n?locale=zh_CN">中文</a> 18 <br><br> 19 <a href="i18n?locale=en_US">英文</a> 20 21 </body> 22 </html>
4.效果