1、在spring.xml中配置html
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 12 <!-- 啓動註解驅動的Spring MVC功能,註冊請求url和註解POJO類方法的映射--> 13 <mvc:annotation-driven /> 14 <!-- 啓動包掃描功能,以便註冊帶有@Controller、@Service、@repository、@Component等註解的類成爲spring的bean --> 15 <context:component-scan base-package="com.**.**" /> 16 <!-- 對模型視圖名稱的解析,在請求時模型視圖名稱添加先後綴 --> 17 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp" /> 18 19 <!-- 根據瀏覽器語言判斷start --> 20 <!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/> --> 21 <!-- 根據瀏覽器語言判斷end --> 22 <!-- ******************************************************************************** --> 23 <!-- 根據session判斷start --> 24 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/> 25 <mvc:interceptors> 26 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 27 </mvc:interceptors> 28 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> 29 <!-- 根據session判斷end --> 30 <!-- ******************************************************************************** --> 31 <!-- 根據cookie判斷start --> 32 <!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/> 33 <mvc:interceptors> 34 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 35 </mvc:interceptors> 36 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> --> 37 <!-- 根據cookie判斷end --> 38 <!-- ******************************************************************************** --> 39 40 </beans>
2、建立三個jsp頁面java
1.根據cookie判斷language_cookie.jsp web
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 5 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 6 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <a href="language_cookie?langType=zh">中文</a> | <a href="language_cookie?langType=en">英文</a><br/> 14 下面展現的是後臺獲取的國際化信息:<br/> 15 ${text}<br/> 16 17 頁面獲取信息:<br/> 18 <spring:message code="text"/> <br/> 19 Locale: ${pageContext.response.locale } 20 </body> 21 </html>
2.根據瀏覽器當前語言判斷language_liulanqi.jspspring
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 5 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 6 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>根據瀏覽器語言獲取</title> 11 </head> 12 <body> 13 後臺獲取的國際化信息:<br/> 14 ${text}<br/> 15 16 頁面獲取信息:<br/> 17 <spring:message code="text"/> <br/> 18 Locale: ${pageContext.response.locale } 19 </body> 20 </html>
3.根據session判斷language_session.jsp瀏覽器
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 5 <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 6 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <a href="language_session?langType=zh">中文</a> | <a href="language_session?langType=en">英文</a><br/> 14 下面展現的是後臺獲取的國際化信息:<br/> 15 ${text}<br/> 16 17 頁面獲取信息:<br/> 18 <spring:message code="text"/> <br/> 19 Locale: ${pageContext.response.locale } 20 </body> 21 </html>
3、配置測試類GlobalController.javaspring-mvc
1 package com.sh.test; 2 3 4 import java.util.Locale; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.context.i18n.LocaleContextHolder; 10 import org.springframework.stereotype.Controller; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RequestMethod; 13 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.servlet.ModelAndView; 15 import org.springframework.web.servlet.i18n.CookieLocaleResolver; 16 import org.springframework.web.servlet.i18n.SessionLocaleResolver; 17 import org.springframework.web.servlet.support.RequestContext; 18 /** 19 * 需在spring-servlet.xml中開啓對應的配置才能生效 20 * @author Administrator 21 * 22 */ 23 @Controller 24 public class GlobalController { 25 26 27 28 /** 29 * 根據瀏覽器語言判斷 30 * @param request 31 * @param response 32 * @param langType 33 * @return 34 */ 35 @RequestMapping(value="/language_liulanqi", method = {RequestMethod.GET}) 36 public ModelAndView test(HttpServletRequest request,HttpServletResponse response){ 37 //從後臺代碼獲取國際化信息 38 RequestContext requestContext = new RequestContext(request); 39 request.setAttribute("text",requestContext.getMessage("text")); 40 return new ModelAndView("/i18n/language_liulanqi"); 41 } 42 /** 43 * 根據session判斷 44 * @param request 45 * @param response 46 * @param langType 47 * @return 48 */ 49 @RequestMapping(value="/language_session", method = {RequestMethod.GET}) 50 public ModelAndView test1(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="langType", defaultValue="zh") String langType){ 51 request.getSession().removeAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME); 52 if(langType.equals("zh")){ 53 Locale locale = new Locale("zh", "CN"); 54 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 55 } 56 else if(langType.equals("en")){ 57 Locale locale = new Locale("en", "US"); 58 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 59 }else{ 60 request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale()); 61 } 62 //從後臺代碼獲取國際化信息 63 RequestContext requestContext = new RequestContext(request); 64 String message = requestContext.getMessage("text"); 65 request.setAttribute("text",message); 66 return new ModelAndView("/i18n/language_session"); 67 } 68 /** 69 * 根據cookie判斷 70 * @param request 71 * @param response 72 * @param langType 73 * @return 74 */ 75 @RequestMapping(value="/language_cookie", method = {RequestMethod.GET}) 76 public ModelAndView test2(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="langType", defaultValue="zh") String langType){ 77 if(langType.equals("zh")){ 78 Locale locale = new Locale("zh", "CN"); 79 (new CookieLocaleResolver()).setLocale (request, response, locale); 80 }else if(langType.equals("en")){ 81 Locale locale = new Locale("en", "US"); 82 (new CookieLocaleResolver()).setLocale (request, response, locale); 83 }else{ 84 (new CookieLocaleResolver()).setLocale (request, response, LocaleContextHolder.getLocale()); 85 } 86 //從後臺代碼獲取國際化信息 87 RequestContext requestContext = new RequestContext(request); 88 request.setAttribute("text",requestContext.getMessage("text")); 89 return new ModelAndView("/i18n/language_cookie"); 90 } 91 92 }
5、在src下建立i18n配置文件,cookie
messages_en_US.propertiessession
text=English2mvc
messages_zh_CN.propertiesapp
text=中文
messages.properties
text=默認
6、使用方式
1.根據瀏覽器語言選擇,須要打開配置文件中的對應配置,開啓此模式後只能經過修改瀏覽器首選語言切換
http://192.168.1.168:8080/i18ntest/language_liulanqi
2.根據session選擇,開啓對應配置,經過URL參數切換語言
http://192.168.1.168:8080/i18ntest/language_session?langType=zh
3.根據cookie選擇,開啓對應配置,經過URL參數切換語言
http://192.168.1.168:8080/i18ntest/language_cookie?langType=en