SpringMVC-国际化
2016-06-07 22:08
417 查看
国际化
-AcceptHeaderLocaleResolver:根据HTTP请求头的Accept-Language参数确定本地化类型,如果没有显示定义本地化解析器,SpringMVC使用该解析器。
-CookieLocaleResolver:根据指定的Cookie值确定本地化类型
-SessionLocaleResolver:根据Session中特定的属性来确定本地化类型
-LocaleChangeInterceptor:从请求参数中获取本次请求的本地化类型
<servlet-name>-servlet.xml
在上面指定了国际化文档的basename名字为 spring.i18n后,在spring目录下创建i18n对应的配置文件。
注意i18n_xx.properties的目录若放置错误,会报错,ResourceBundle [spring.i18n] not found for MessageSource
1)i18n_en_US.properties
i18n.name=Name
i18n.age=Age
2)i18n_zh_CN.properties //命名中文的名字,年龄
i18n.name=\u540D\u5B57
i18n.age=\u5E74\u9F84
配置好后,就可以在对应的jsp文档中使用数据。
由于新版本的SpringMVC进行了更新,如果直接使用SessionLocaleResolver会报如下错误,所以真正实现通过超链接切换中英文,需要通过子类覆盖报错父类方法。
java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategy
在上面的配置文件基础上追加如下配置
注意locale需要全部小写。
<完>
-AcceptHeaderLocaleResolver:根据HTTP请求头的Accept-Language参数确定本地化类型,如果没有显示定义本地化解析器,SpringMVC使用该解析器。
-CookieLocaleResolver:根据指定的Cookie值确定本地化类型
-SessionLocaleResolver:根据Session中特定的属性来确定本地化类型
-LocaleChangeInterceptor:从请求参数中获取本次请求的本地化类型
1.根据浏览器语言设置,默认选择执行本地化操作。
JSTL的fmt标签实现。<servlet-name>-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="spring"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="spring.i18n"></property> </bean> <mvc:view-controller path="/i18n" view-name="i18n"/> <mvc:default-servlet-handler/> <mvc:annotation-driven></mvc:annotation-driven> </beans>
在上面指定了国际化文档的basename名字为 spring.i18n后,在spring目录下创建i18n对应的配置文件。
注意i18n_xx.properties的目录若放置错误,会报错,ResourceBundle [spring.i18n] not found for MessageSource
1)i18n_en_US.properties
i18n.name=Name
i18n.age=Age
2)i18n_zh_CN.properties //命名中文的名字,年龄
i18n.name=\u540D\u5B57
i18n.age=\u5E74\u9F84
配置好后,就可以在对应的jsp文档中使用数据。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <fmt:message key="i18n.name"></fmt:message> <fmt:message key="i18n.age"></fmt:message> </body> </html>
2.通过超链接切换本地化。
配置LocalResolver,和LocaleChangeInterceptor来实现。由于新版本的SpringMVC进行了更新,如果直接使用SessionLocaleResolver会报如下错误,所以真正实现通过超链接切换中英文,需要通过子类覆盖报错父类方法。
java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategy
在上面的配置文件基础上追加如下配置
<bean id="localResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>jsp代码实现如下:
注意locale需要全部小写。
<a href="i18n?locale=zh_CH">中文</a><br> <a href="i18n?locale=us_EN">英文</a><br> <fmt:message key="i18n.name"></fmt:message> <fmt:message key="i18n.age"></fmt:message>
<完>
相关文章推荐
- SpringMVC学习--功能完善
- 10043---java动态代理(cglib)
- Spring 事务传播机制 实例讲解
- SpringMVC学习系列-后记 解决GET请求时中文乱码的问题
- Java Instrument (三) 钩子函数
- 01-java中“接口”的概念
- SpringMVC中文乱码 controller乱码
- "快速排序"算法-之通俗易懂全面解析
- Java Instrument (二) JVMTI
- java多线程基础二
- springmvc和json整合配置方法
- 读写锁ReentrantReadWriteLock
- 《Java编程思想》阅读笔记一
- leetcode-java-16. 3Sum Closest
- Java中toString()用法
- 在web.xml中配置diaplay-name是什么意思
- Struts2源码分析——StrutsPrepareAndExecuteFilter
- javaweb学习总结(四十五)——监听器(Listener)学习二
- java读取文件 每行首字丢失问题
- java中util.Date和数据库中datetime的操作!