欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)服務(wù),我們努力開拓更好的視野,通過(guò)不懈的努力,創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵(lì)著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),成都小程序開發(fā),網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫(kù)的技術(shù)開發(fā)工程師。

一.基于瀏覽器請(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è)試:

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

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

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

二.基于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è)試:

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

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

三.基于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è)試:

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

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

關(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可見的地址,默認(rèn)是“/”即對(duì)網(wǎng)站所有地址都是可見的,如果設(shè)為其它地址,則只有該地址或其后的地址才可見-->
  <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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前名稱:詳解SpringMVC學(xué)習(xí)系列之國(guó)際化
路徑分享:http://aaarwkj.com/article18/peisdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、外貿(mào)建站、品牌網(wǎng)站設(shè)計(jì)ChatGPT、微信公眾號(hào)、域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
国产亚洲精品视频二区| 人妻免费精品久久一区| 亚洲第一女人天堂av| 中文字幕中文字幕乱码| 黑人巨大精品欧美黑寡妇| 亚洲大陆免费在线视频| 日日躁夜夜躁狠狠躁黑人| 亚洲伦理一区二区三区中文| 欧美日韩精品人妻二区| 99久久热这里只有精品| 99久久热这里只有精品| 日本av在线中文一区二区| 欧美三级影院网上在线| 丰满人妻被黑人猛烈进入免费| 国产精品日产三级在线观看| 精品一二三区在线天堂| 日本精品视频一区二区三区| 在线观看免费国产b片| 性生活视性生活大片日本| 四虎在线观看最新免费| 18禁黄久久久一区二区三区| 国产精品亚洲欧美在线| 成人免费毛片内射视频| 日韩中文字幕一二三| 91大片在线观看视频| 亚洲激情视频久久精品| 久久国产精品午夜亚洲欧美| 欧美日韩亚洲精品内裤| 国产成人久久精品二区三区| 欧美日韩在线视频一区| 国产成人午夜视频免费一区| 情五月激情亚洲丁香佳色| 九九视频免费观看5| 久久精品国产精品日韩欧美| 好看毛片一区二区三区四区| 亚洲综合一区二区精品久久| 91中文在线视频播放| 精品欧美一区二区精品| 亚洲精品欧美日韩久久| 亚州精品少妇久久久久久| 欧美日韩精品视频专区|