激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springboot+thymeleaf國際化之LocaleResolver接口的示例

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

2021-02-01 12:04幽魂步 Java教程

本篇文章主要介紹了springboot+thymeleaf國際化之LocaleResolver的示例 ,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

spring采用的默認區域解析器是AcceptHeaderLocaleResolver,根據request header中的accept-language值來解析locale,并且是不可變的。

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

我就以SessionLocaleResolver舉例:

1.建立一個配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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" 是必須的

優先級如下:

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

如果沒有但是SessionLocaleResolver設置了默認的locale則按默認值來

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

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

2.建立對應的messages.properties

messages.properties

messages_en.properties

messages_zh_CN.properties

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

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

msg.properties

msg_en.properties

msg_zh_CN.properties

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

3.controller中切換locale 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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中默認屬性名

可以看一下SessionLocaleResolver的部分源碼

?
1
2
3
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默認屬性名為

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

setLocale是抽象類AbstractLocaleContextResolver中方法

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

然后看SessionLocaleResolver中setLocaleContext 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}

本質上就是一些非空判斷取默認,最終給session中的對應屬性賦值

4.thymeleaf頁面中調用

?
1
2
3
4
5
6
7
8
9
10
11
<!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就煩!!!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/wqbill/p/5773338.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费a视频在线观看 | 又黄又爽免费无遮挡在线观看 | av观看国产| 久久久久国产一区二区三区不卡 | 欧美一页| 午夜a狂野欧美一区二区 | 日韩精品久久久久久久电影99爱 | 亚洲视频在线网 | 中文在线观看视频 | 久久久裸体视频 | xxxxhdhdhdhd日本 | 欧美福利视频一区二区 | 在线天堂中文在线资源网 | 国产视频在线一区 | 国产区二区 | 成人做爰s片免费看网站 | 欧美成人激情 | 国产精品久久久久久久久久久久久久久久 | 手机国产乱子伦精品视频 | 黄色毛片一级 | 久久久大片| 亚洲综合视频网 | 欧美一级视频网站 | 国产亚洲精品综合一区91 | 久久精品国产精品亚洲 | 黄色久 | 49vv看片免费| 欧美一级黄色网 | a黄色网 | 久久亚洲精品久久国产一区二区 | 精品国产乱码久久久久久久 | 免费嗨片首页中文字幕 | 性爱视频免费 | 日日草天天干 | 91短视频在线免费观看 | 欧美国产日韩在线观看成人 | 日本不卡一区二区三区在线 | 国产精品呻吟 | 黄色av.com | 日本一区视频在线观看 | 久久久久久艹 |