八、springMVC之国际化
一、关于国际化使用场景:
1.在页面上能够根据浏览器语言设置的情况对文本(不是内容),时间,数值进行本地化处理,显示对应语言的版本;
2.可以在bean中获取国际化资源文件LOCAL对应的消息,即在bean中获取当前消息,对应的语言版本;
3.可以通过超链接切换locale,而不再依赖浏览器的语言设置情况
二、场景实现
1.目录结构:

2.配置
web.xml:只配置了springDispatcherServlet;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 <servlet> 7 <servlet-name>springDispatcherServletservlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 9 10 12 <init-param> 13 <param-name>contextConfigLocationparam-name> 14 <param-value>classpath:spring.xmlparam-value> 15 init-param> 16 17 <load-on-startup>1load-on-startup> 18 servlet> 19 20 21 <servlet-mapping> 22 <servlet-name>springDispatcherServletservlet-name> 23 <url-pattern>/url-pattern> 24 servlet-mapping> 25 web-app>
spring.xml基础配置(适用于场景1、2):
包扫描的配置、视图解析器配置,国际化资源文件配置、<mvc:annotation-driven>mvc:annotation-driven>;
1 <context:component-scan base-package="handler">context:component-scan> 2 3 <bean 4 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 5 <property name="prefix" value="/">property> 6 <property name="suffix" value=".jsp">property> 7 bean> 8 9 <bean id="messageSource" 10 class="org.springframework.context.support.ResourceBundleMessageSource"> 11 <property name="basename" value="message">property> 12 bean> 13 14 <mvc:annotation-driven>mvc:annotation-driven>
spring.xml配置(适用于场景3):
实现 通过超链接切换locale,而不再依赖浏览器的语言设置,需要配置
1 2 <bean id="localeResolver" 3 class="org.springframework.web.servlet.i18n.SessionLocaleResolver">bean> 4 5 <mvc:interceptors> 6 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> 7 mvc:interceptors>
完整配置为:
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:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="handler">context:component-scan> 11 12 <bean 13 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/">property> 15 <property name="suffix" value=".jsp">property> 16 bean> 17 18 <bean id="messageSource" 19 class="org.springframework.context.support.ResourceBundleMessageSource"> 20 <property name="basename" value="message">property> 21 bean> 22 23 <mvc:annotation-driven>mvc:annotation-driven> 24 25 26 <bean id="localeResolver" 27 class="org.springframework.web.servlet.i18n.SessionLocaleResolver">bean> 28 29 <mvc:interceptors> 30 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> 31 mvc:interceptors> 32 33 beans>
3.国际化文件message_en_US.properties&message_zh_CN.properties
1 I18N.TEST_EN_US=TEST_EN_US
I18N.TEST_EN_US = 测试语言
4.EmployeeController.java
1 package handler; 2 3 import java.util.Locale; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.support.ResourceBundleMessageSource; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 @RequestMapping("/emp") 11 @Controller 12 public class EmployeeController { 13 @Autowired 14 private ResourceBundleMessageSource messageSource; 15 16 @RequestMapping("/testLang1") 17 public String testLang1() { 18 return "forward:/index.jsp"; 19 } 20 21 @RequestMapping("/testLang") 22 public String testLang(Locale locale) { 23 String val = messageSource.getMessage("I18N.TEST_EN_US", null, locale); 24 System.out.println(val); 25 return "forward:/index.jsp"; 26 } 27 28 }
5.index.jsp
分别使用 fmt 和spring tag;两种标签实现的结果是一致的,可以任意选择一种;
场景1、2测试页面;
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@page import="java.util.*"%> 4 <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%> 5 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 6 DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8"> 10 <title>Insert title heretitle> 11 head> 12 <body> 13 <p> 14 <fmt:bundle basename="message"> 15 <fmt:message key="I18N.TEST_EN_US">fmt:message> 16 fmt:bundle> 17 p> 18 19 <p><s:message code="I18N.TEST_EN_US">s:message>p> 20 21 body> 22 html>
场景3的测试页面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@page import="java.util.*"%> 4 <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%> 5 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 6 DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8"> 10 <title>Insert title heretitle> 11 head> 12 <body> 13 <p> 14 <fmt:setLocale value="${param.locale}"/> 15 <fmt:bundle basename="message"> 16 <fmt:message key="I18N.TEST_EN_US">fmt:message> 17 fmt:bundle> 18 p> 19 20 <p><s:message code="I18N.TEST_EN_US">s:message>p> 21 22 body> 23 html>
运行结果:
场景1,2的配置方式下的运行结果:
访问:http://localhost:8080/DataOperate/emp/testLang1,此时chrome浏览器语言为中文,测试页面显示中文,如下图且控制台;语言切换到英语时,显示为英语;,显示如下图;
访问:http://localhost:8080/DataOperate/emp/testLang,此时chrome浏览器语言为中文,测试页面显示中文且控制台TEST_EN_US,语言切换到英语时,显示为英语,且控制台打印:测试语言;


场景3的配置方式下的运行结果:
访问http://localhost:8080/DataOperate/emp/testLang?locale=en_US 页面显示英文;访问http://localhost:8080/DataOperate/emp/testLang?locale=zh_CN 页面显示中文;
