国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 天天躁日日躁bbbbb | 国产日韩精品一区 | 国产免费视频 | 天天射天天干 | 成人片免费看 | 中文字幕国产一区 | 97热在线观看 | 中文字幕在线播放一区 | 香蕉视频成人在线观看 | 视频一区免费观看 | 精品久久国产 | 久久久中文 | 91精品一区二区三区久久久久久 | 国产免费一区二区 | av在线电影观看 | 中文字幕电影在线观看 | 亚洲青青草 | 一区二区色 | 欧美1级| 日韩不卡二区 | 欧美成人免费 | 免费观看的黄色 | 成人午夜影院 | 欧美一级片免费在线观看 | 午夜大片网| 久久精品久久久 | 91精品国产综合久久久久久 | 狠狠躁夜夜躁人人爽天天天天97 | 亚洲综合首页 | 日韩在线视频免费观看 | www中文字幕 | 日韩一区电影 | 亚洲一区二区三区在线 | 久久99精品久久久久久国产越南 | 天堂av资源| 中文字幕亚洲欧美日韩在线不卡 | 精品亚洲一区二区 | 午夜影院在线 | 午夜精品美女久久久久av福利 | 成人国产精品久久 | 动漫爱爱视频 |