欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot+thymeleaf國際化之LocaleResolver接口的示例

 更新時間:2017年11月08日 14:22:30   作者:幽魂步  
本篇文章主要介紹了springboot+thymeleaf國際化之LocaleResolver的示例 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

springboot中大部分有默認(rèn)配置所以開發(fā)起項目來非常迅速,僅對需求項做單獨配置覆蓋即可

spring采用的默認(rèn)區(qū)域解析器是AcceptHeaderLocaleResolver,根據(jù)request header中的accept-language值來解析locale,并且是不可變的。

那么想要實現(xiàn)國際化,就要使用SessionLocaleResolver或者CookieLocaleResolver。正如類的名字所示,是按session或cookie中儲存的locale值來解析locale。

我就以SessionLocaleResolver舉例:

1.建立一個配置類

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * Created by wq on 2016/8/15.
 */
@Configuration
public class SpringMVC_config {
  @Bean(name="localeResolver")
  public LocaleResolver localeResolverBean() {
    return new SessionLocaleResolver();
  }
//  @Bean(name="messageSource")
//  public ResourceBundleMessageSource resourceBundleMessageSource(){
//    ResourceBundleMessageSource source=new ResourceBundleMessageSource();
//    source.setBasename("messages");
//    return source;
//  }
}

注意 name="localeResolver" 是必須的

優(yōu)先級如下:

session中對應(yīng)屬性(3中有說明)有值則按session來

如果沒有但是SessionLocaleResolver設(shè)置了默認(rèn)的locale則按默認(rèn)值來

//    SessionLocaleResolver localeResolver=new SessionLocaleResolver();
//    localeResolver.setDefaultLocale(Locale.ENGLISH);

再然后就還是按request header中的accept-language值來

2.建立對應(yīng)的messages.properties

messages.properties

messages_en.properties

messages_zh_CN.properties

前面注釋的代碼則可以修改properties的前綴部分,name="messageSource" 同樣是必須的

比如 setBasename("msg"); 對應(yīng)properties則為

msg.properties

msg_en.properties

msg_zh_CN.properties

格式上sys.test=hello、sys.test=你好,應(yīng)該無需贅述(可能轉(zhuǎn)碼會避免一些問題,我這里直接放的中文)

3.controller中切換locale 

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;

import static org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME;

/**
 * Created by Administrator on 2016/6/11.
 */
@Controller
public class DemoController {
  @Autowired
  LocaleResolver localeResolver;

  @RequestMapping("test")
  public String test(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session=request.getSession();
    localeResolver.setLocale(request,response,Locale.ENGLISH);
    System.out.println(session.getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME));
    return "messages";
  }
}

這里org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME這個字符串常量則是session中默認(rèn)屬性名

可以看一下SessionLocaleResolver的部分源碼

public class SessionLocaleResolver extends AbstractLocaleContextResolver {
  public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";
  public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".TIME_ZONE";

locale默認(rèn)屬性名為

org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE

setLocale是抽象類AbstractLocaleContextResolver中方法

  public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    this.setLocaleContext(request, response, locale != null?new SimpleLocaleContext(locale):null);
  }

然后看SessionLocaleResolver中setLocaleContext 

  public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
    Locale locale = null;
    TimeZone timeZone = null;
    if(localeContext != null) {
      locale = localeContext.getLocale();
      if(localeContext instanceof TimeZoneAwareLocaleContext) {
        timeZone = ((TimeZoneAwareLocaleContext)localeContext).getTimeZone();
      }
    }

    WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
    WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
  }

本質(zhì)上就是一些非空判斷取默認(rèn),最終給session中的對應(yīng)屬性賦值

4.thymeleaf頁面中調(diào)用

<!DOCTYPE html>
<html lang="zh_CN"
   xmlns:th="http://www.thymeleaf.org">
<head>
  <title>msg</title>
</head>
<body>
<h1 th:text="${#locale}"></h1>
<h1 th:text="#{sys.test}"></h1>
</body>
</html>

則顯示en hello

能用注解的,盡量不用xml,看著xml就煩?。?!

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

相關(guān)文章

  • spring boot配置ssl實現(xiàn)HTTPS的方法

    spring boot配置ssl實現(xiàn)HTTPS的方法

    這篇文章主要介紹了spring boot配置ssl實現(xiàn)HTTPS的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • MyBatis-Plus常見面試題和答案大全

    MyBatis-Plus常見面試題和答案大全

    Mybatis-Plus是一個基于Mybatis的增強工具,它簡化了Mybatis的開發(fā)流程,提供了許多實用的功能,如自動生成代碼、分頁查詢、條件構(gòu)造器、性能分析等,這篇文章主要給大家介紹了關(guān)于MyBatis-Plus常見面試題和答案的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Java線程編程中isAlive()和join()的使用詳解

    Java線程編程中isAlive()和join()的使用詳解

    這篇文章主要介紹了Java線程編程中isAlive()和join()的使用詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • Java中Properties類的操作實例詳解

    Java中Properties類的操作實例詳解

    這篇文章主要介紹了Java中Properties類的操作實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實踐與指導(dǎo)詳解

    圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實踐與指導(dǎo)詳解

    這篇文章主要介紹了圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實踐與指導(dǎo)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • java使用EasyExcel導(dǎo)入導(dǎo)出excel

    java使用EasyExcel導(dǎo)入導(dǎo)出excel

    導(dǎo)入導(dǎo)出excel數(shù)據(jù)是常見的需求,今天就來看一下Java基于EasyExcel實現(xiàn)這個功能,感興趣的朋友可以了解下
    2021-05-05
  • MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式

    MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式

    這篇文章主要介紹了MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java使用substring()截取(提取)子字符串

    Java使用substring()截取(提取)子字符串

    這篇文章主要介紹了Java使用substring()截取(提取)子字符串,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • java為什么不建議用equals判斷對象相等

    java為什么不建議用equals判斷對象相等

    本文主要介紹了java為什么不建議用equals判斷對象相等,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • java之Thread不捕獲異常默認(rèn)處理邏輯

    java之Thread不捕獲異常默認(rèn)處理邏輯

    這篇文章主要介紹了java之Thread不捕獲異常默認(rèn)處理邏輯,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評論