最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringMVC學(xué)習(xí)系列之國(guó)際化

 更新時(shí)間:2017年07月03日 10:41:17   作者:Erola  
這篇文章主要介紹了詳解SpringMVC學(xué)習(xí)系列之國(guó)際化,詳細(xì)的介紹了關(guān)于瀏覽器,Session,Cookie,URL請(qǐng)求的國(guó)際化的實(shí)現(xiàn),有興趣的可以了解一下

在系列(7)中我們講了數(shù)據(jù)的格式化顯示,Spring在做格式化展示的時(shí)候已經(jīng)做了國(guó)際化處理,那么如何將我們網(wǎng)站的其它內(nèi)容(如菜單、標(biāo)題等)做國(guó)際化處理呢?這就是本篇要將的內(nèi)容—>國(guó)際化。

一.基于瀏覽器請(qǐng)求的國(guó)際化實(shí)現(xiàn):

首先配置我們項(xiàng)目的springservlet-config.xml文件添加的內(nèi)容如下:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <!-- 國(guó)際化信息所在的文件名 -->           
  <property name="basename" value="messages" />  
  <!-- 如果在國(guó)際化資源文件中找不到對(duì)應(yīng)代碼的信息,就用這個(gè)代碼作為名稱 -->        
  <property name="useCodeAsDefaultMessage" value="true" />      
</bean>

在com.demo.web.controllers包中添加GlobalController.java內(nèi)容如下:

package com.demo.web.controllers;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.support.RequestContext;
import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")
public class GlobalController {
  
  @RequestMapping(value="/test", method = {RequestMethod.GET})
  public String test(HttpServletRequest request,Model model){
    if(!model.containsAttribute("contentModel")){
      
      //從后臺(tái)代碼獲取國(guó)際化信息
      RequestContext requestContext = new RequestContext(request);
      model.addAttribute("money", requestContext.getMessage("money"));
      model.addAttribute("date", requestContext.getMessage("date"));

      
      FormatModel formatModel=new FormatModel();

      formatModel.setMoney(12345.678);
      formatModel.setDate(new Date());
      
      model.addAttribute("contentModel", formatModel);
    }
    return "globaltest";
  }
  
}

這里展示模型還用系列(7)中的作為演示。

在項(xiàng)目中的源文件夾resources中添加messages.properties、messages_zh_CN.properties、messages_en_US.properties三個(gè)文件,其中messages.properties、messages_zh_CN.properties里面的"money", "date",為中文,messages_en_US.properties里面的為英文。

在views文件夾中添加globaltest.jsp視圖,內(nèi)容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

 
  下面展示的是后臺(tái)獲取的國(guó)際化信息:<br/>
  ${money}<br/>
  ${date}<br/>

  下面展示的是視圖中直接綁定的國(guó)際化信息:<br/>
  <spring:message code="money"/>:<br/>
  <spring:eval expression="contentModel.money"></spring:eval><br/>
  <spring:message code="date"/>:<br/>
  <spring:eval expression="contentModel.date"></spring:eval><br/>
  
</body>
</html>

運(yùn)行測(cè)試:

更改瀏覽器語(yǔ)言順序,刷新頁(yè)面:

二.基于Session的國(guó)際化實(shí)現(xiàn):

在項(xiàng)目的springservlet-config.xml文件添加的內(nèi)容如下(第一種時(shí)添加的內(nèi)容要保留):

<mvc:interceptors> 
  <!-- 國(guó)際化操作攔截器 如果采用基于(請(qǐng)求/Session/Cookie)則必需配置 --> 
  <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 
</mvc:interceptors> 

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

更改globaltest.jsp視圖為如下內(nèi)容:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <a href="test?langType=zh" rel="external nofollow" >中文</a> | <a href="test?langType=en" rel="external nofollow" >英文</a><br/>

  下面展示的是后臺(tái)獲取的國(guó)際化信息:<br/>
  ${money}<br/>
  ${date}<br/>

  下面展示的是視圖中直接綁定的國(guó)際化信息:<br/>
  <spring:message code="money"/>:<br/>
  <spring:eval expression="contentModel.money"></spring:eval><br/>
  <spring:message code="date"/>:<br/>
  <spring:eval expression="contentModel.date"></spring:eval><br/>
  
</body>
</html>

更改GlobalController.java為如下內(nèi)容:

package com.demo.web.controllers;

import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContext;
import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")
public class GlobalController {
  
  @RequestMapping(value="/test", method = {RequestMethod.GET})
  public String test(HttpServletRequest request,Model model, @RequestParam(value="langType", defaultValue="zh") String langType){
    if(!model.containsAttribute("contentModel")){
      
      if(langType.equals("zh")){
        Locale locale = new Locale("zh", "CN"); 
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
      }
      else if(langType.equals("en")){
        Locale locale = new Locale("en", "US"); 
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
      }
      else 
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
      
      //從后臺(tái)代碼獲取國(guó)際化信息
      RequestContext requestContext = new RequestContext(request);
      model.addAttribute("money", requestContext.getMessage("money"));
      model.addAttribute("date", requestContext.getMessage("date"));

      
      FormatModel formatModel=new FormatModel();

      formatModel.setMoney(12345.678);
      formatModel.setDate(new Date());
      
      model.addAttribute("contentModel", formatModel);
    }
    return "globaltest";
  }
  
}

運(yùn)行測(cè)試:

三.基于Cookie的國(guó)際化實(shí)現(xiàn):

把實(shí)現(xiàn)第二種方法時(shí)在項(xiàng)目的springservlet-config.xml文件中添加的

復(fù)制代碼 代碼如下:

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

注釋掉,并添加以下內(nèi)容:

復(fù)制代碼 代碼如下:

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

更改GlobalController.java為如下內(nèi)容:

package com.demo.web.controllers;

import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
//import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContext;

import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")
public class GlobalController {
  
  @RequestMapping(value="/test", method = {RequestMethod.GET})
  public String test(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam(value="langType", defaultValue="zh") String langType){
    if(!model.containsAttribute("contentModel")){
      
      /*if(langType.equals("zh")){
        Locale locale = new Locale("zh", "CN"); 
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
      }
      else if(langType.equals("en")){
        Locale locale = new Locale("en", "US"); 
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
      }
      else 
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());*/
      
      if(langType.equals("zh")){
        Locale locale = new Locale("zh", "CN"); 
        //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
        (new CookieLocaleResolver()).setLocale (request, response, locale);
      }
      else if(langType.equals("en")){
        Locale locale = new Locale("en", "US"); 
        //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
        (new CookieLocaleResolver()).setLocale (request, response, locale);
      }
      else 
        //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
        (new CookieLocaleResolver()).setLocale (request, response, LocaleContextHolder.getLocale());
      
      //從后臺(tái)代碼獲取國(guó)際化信息
      RequestContext requestContext = new RequestContext(request);
      model.addAttribute("money", requestContext.getMessage("money"));
      model.addAttribute("date", requestContext.getMessage("date"));

      
      FormatModel formatModel=new FormatModel();

      formatModel.setMoney(12345.678);
      formatModel.setDate(new Date());
      
      model.addAttribute("contentModel", formatModel);
    }
    return "globaltest";
  }
  
}

運(yùn)行測(cè)試:

關(guān)于<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />3個(gè)屬性的說(shuō)明(可以都不設(shè)置而用其默認(rèn)值):

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
  <!-- 設(shè)置cookieName名稱,可以根據(jù)名稱通過(guò)js來(lái)修改設(shè)置,也可以像上面演示的那樣修改設(shè)置,默認(rèn)的名稱為 類名+LOCALE(即:org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE-->
  <property name="cookieName" value="lang"/>
  <!-- 設(shè)置最大有效時(shí)間,如果是-1,則不存儲(chǔ),瀏覽器關(guān)閉后即失效,默認(rèn)為Integer.MAX_INT-->
  <property name="cookieMaxAge" value="100000">
  <!-- 設(shè)置cookie可見(jiàn)的地址,默認(rèn)是“/”即對(duì)網(wǎng)站所有地址都是可見(jiàn)的,如果設(shè)為其它地址,則只有該地址或其后的地址才可見(jiàn)-->
  <property name="cookiePath" value="/">
</bean>

四.基于URL請(qǐng)求的國(guó)際化的實(shí)現(xiàn):

首先添加一個(gè)類,內(nèi)容如下:

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;

public class MyAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver {

  private Locale myLocal;

  public Locale resolveLocale(HttpServletRequest request) {
    return myLocal;
  } 

  public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    myLocal = locale;
  }
 
}

然后把實(shí)現(xiàn)第二種方法時(shí)在項(xiàng)目的springservlet-config.xml文件中添加的

復(fù)制代碼 代碼如下:

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

注釋掉,并添加以下內(nèi)容:

<bean id="localeResolver" class="xx.xxx.xxx.MyAcceptHeaderLocaleResolver"/>

“xx.xxx.xxx”是剛才添加的MyAcceptHeaderLocaleResolver 類所在的包名。

保存之后就可以在請(qǐng)求的URL后附上 locale=zh_CN 或 locale=en_US 如 http://xxxxxxxx?locale=zh_CN 來(lái)改變語(yǔ)言了,具體這里不再做演示了。

國(guó)際化部分的內(nèi)容到此結(jié)束。 

代碼下載:SpringMVCi18n_jb51.rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot+vue+Axios實(shí)現(xiàn)Token令牌的詳細(xì)過(guò)程

    SpringBoot+vue+Axios實(shí)現(xiàn)Token令牌的詳細(xì)過(guò)程

    Token是在服務(wù)端產(chǎn)生的,前端可以使用用戶名/密碼向服務(wù)端請(qǐng)求認(rèn)證(登錄),服務(wù)端認(rèn)證成功,服務(wù)端會(huì)返回?Token?給前端,Token可以使用自己的算法自定義,本文給大家介紹SpringBoot+vue+Axios實(shí)現(xiàn)Token令牌,感興趣的朋友一起看看吧
    2023-10-10
  • 基于springboot2集成jpa,創(chuàng)建dao的案例

    基于springboot2集成jpa,創(chuàng)建dao的案例

    這篇文章主要介紹了基于springboot2集成jpa,創(chuàng)建dao的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • mybatis-plus多表關(guān)聯(lián)查詢功能的實(shí)現(xiàn)

    mybatis-plus多表關(guān)聯(lián)查詢功能的實(shí)現(xiàn)

    本文給大家介紹mybatis-plus多表關(guān)聯(lián)查詢功能的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • 詳解JDK 5 Annotation 注解之@Target的用法介紹

    詳解JDK 5 Annotation 注解之@Target的用法介紹

    這篇文章主要介紹了詳解JDK 5 Annotation 注解之@Target的用法介紹,需要的朋友可以參考下
    2016-02-02
  • springboot查詢?nèi)坎块T流程分析

    springboot查詢?nèi)坎块T流程分析

    本文分析了在SpringBoot框架中前端如何請(qǐng)求DeptController的list()方法,并通過(guò)DeptService到DeptMapper接口查詢數(shù)據(jù)庫(kù)中的全部部門信息的流程,整個(gè)過(guò)程涉及前端到后端數(shù)據(jù)的獲取和返回,是SpringBoot應(yīng)用中常見(jiàn)的數(shù)據(jù)處理模式
    2024-10-10
  • IDEA部署JavaWeb項(xiàng)目到Tomcat服務(wù)器的方法

    IDEA部署JavaWeb項(xiàng)目到Tomcat服務(wù)器的方法

    這篇文章主要介紹了IDEA部署JavaWeb項(xiàng)目到Tomcat服務(wù)器的方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-06-06
  • Security6.4.2?自定義異常中統(tǒng)一響應(yīng)遇到的問(wèn)題

    Security6.4.2?自定義異常中統(tǒng)一響應(yīng)遇到的問(wèn)題

    本文主要介紹了Security6.4.2?自定義異常中統(tǒng)一響應(yīng)遇到的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • java計(jì)算代碼段執(zhí)行時(shí)間的詳細(xì)代碼

    java計(jì)算代碼段執(zhí)行時(shí)間的詳細(xì)代碼

    java里計(jì)算代碼段執(zhí)行時(shí)間可以有兩種方法,一種是毫秒級(jí)別的計(jì)算,另一種是更精確的納秒級(jí)別的計(jì)算,這篇文章主要介紹了java計(jì)算代碼段執(zhí)行時(shí)間,需要的朋友可以參考下
    2022-08-08
  • 百度Java面試題 前200頁(yè)精選(下)

    百度Java面試題 前200頁(yè)精選(下)

    這篇文章主要為大家分享了Java面試資源下篇,百度“Java面試題”前200頁(yè)都在這里了,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Spring Cloud之注冊(cè)中心Nacos的使用詳解

    Spring Cloud之注冊(cè)中心Nacos的使用詳解

    本文介紹SpringCloud Alibaba中的Nacos組件,對(duì)比了Nacos與Eureka的區(qū)別,展示了如何在項(xiàng)目中引入SpringCloud Alibaba和Nacos,并配置負(fù)載均衡,通過(guò)實(shí)際操作,驗(yàn)證了服務(wù)注冊(cè)和負(fù)載均衡的功能,感興趣的朋友一起看看吧
    2025-03-03

最新評(píng)論

青州市| 腾冲县| 贞丰县| 忻城县| 安远县| 景东| 左权县| 比如县| 永济市| 潜江市| 江川县| 宜兰县| 新邵县| 安岳县| 吉木萨尔县| 巴青县| 滦平县| 隆化县| 嘉定区| 开封市| 禹州市| 壤塘县| 延川县| 教育| 镇赉县| 克什克腾旗| 麻城市| 乐都县| 平昌县| 老河口市| 洛川县| 当雄县| 巨野县| 简阳市| 紫金县| 年辖:市辖区| 子洲县| 汶川县| 宁波市| 青阳县| 富阳市|