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

springboot頁面國際化配置指南

 更新時間:2022年03月04日 16:37:29   作者:阿落小世界  
聽起來高大上的國際化,起始就是在利用瀏覽器語言,或者頁面中的中英文切換,將頁面的文字在其他語言和中文進行切換,這篇文章主要給大家介紹了關(guān)于springboot頁面國際化配置的相關(guān)資料,需要的朋友可以參考下

前言

前一段時間做了一個項目,需要解決中文、繁體、英文的國際化問題,所以本文將詳細(xì)介紹springboot頁面國際化配置的過程

方法如下

1.引入依賴pom.xml

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.導(dǎo)入網(wǎng)頁資源,這里給大家推薦一個我自己在使用的頁面資源,SB ADMIN-2

html頁面放在templates目錄下,這是thymeleaf默認(rèn)的解析目錄,其他的樣式文件放在static目錄下

3.接管spring Mvc,自定義url訪問路徑,可做可不做

建一個config目錄,在這里建一個myWebMvcConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class myWebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/wq").setViewName("register");//localhost:8080/wq
        registry.addViewController("/").setViewName("register");//localhpst:8080/
        registry.addViewController("/register.html").setViewName("register");
        //localhost:8080/register.html
    }
}

路徑可以設(shè)置多個,這樣只要是這三個url,spring 都會訪問register.html

還有一種方式也能實現(xiàn)

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class demoController {
    @RequestMapping({"/","/wq"})
    public String test(){
        return "register";
    }
}

4.國際化配置文件:en_US英文,zh_CN中文

點擊左上角加號,便可以添加配置的屬性,只要在右邊填寫相應(yīng)的中英文即可

5. 配置文件已經(jīng)寫好,如何在我們的頁面中使用呢?thyme leaf的作用又來了

首先在你的網(wǎng)頁添加這樣的頭部

<html lang="en" xmlns:th="http://www.thymeleaf.org">

在所有的html屬性前加**th:**就被thymeleaf接管了,根據(jù)thymeleaf 語法,獲取國際化值使用**#{}**,本地值用**${}**,url用**@{}**

? ?<a ?th:href="@{/register.html(l='zh_CN')}" rel="external nofollow" ?>中文 </a>
? <a ?th:href="@{/register.html(l='en_US')}" rel="external nofollow" >English </a>

6. 頁面和配置文件都準(zhǔn)備好了,怎樣實現(xiàn)跳轉(zhuǎn)呢?

 在WebMvcAutoConfiguration.class中

? ? ? ?? ??? ?@Bean
? ? ? ? ? ? ?@ConditionalOnMissingBean(
? ? ? ? ? ? ? ? ?name = {"localeResolver"}
? ? ? ? ? ? ?)
? ? ? ? ? ? ?public LocaleResolver localeResolver() {
? ? ? ? ? ? ? ? ?if (this.webProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.WebProperties.LocaleResolver.FIXED) {
? ? ? ? ? ? ? ? ? ? ?return new FixedLocaleResolver(this.webProperties.getLocale());
? ? ? ? ? ? ? ? ?} else {
? ? ? ? ? ? ? ? ? ? ?AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
? ? ? ? ? ? ? ? ? ? ?localeResolver.setDefaultLocale(this.webProperties.getLocale());
? ? ? ? ? ? ? ? ? ? ?return localeResolver;
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ?}

我們再找到AcceptHeaderLocaleResolver.class,發(fā)現(xiàn)它實現(xiàn)了LocaleResolver 

? ? ?public class AcceptHeaderLocaleResolver implements LocaleResolver {
? ? ? ? ?private final List<Locale> supportedLocales = new ArrayList(4);
? ? ? ? ?@Nullable
? ? ? ? ?private Locale defaultLocale;

那我們就編寫自己的LocaleResolver 

? ? ?public class myLocaleResolver implements LocaleResolver {
? ? ? ? ?@Override
? ? ? ? ?public Locale resolveLocale(HttpServletRequest request) {
? ? ?
? ? ? ? ? ? ?String mylocale=request.getParameter("l");
? ? ? ? ? ? ?Locale locale=Locale.getDefault();
? ? ? ? ? ? ?if(!StringUtils.isEmpty(mylocale)){
? ? ? ? ? ? ? ? ?String[] split=mylocale.split("_");
? ? ? ? ? ? ? ? ?locale=new Locale(split[0],split[1]);
? ? ? ? ? ? ?}
? ? ? ? ? ? ? System.out.println("debug====>"+mylocale);
? ? ? ? ? ? ?return locale;
? ? ? ? ?}
? ? ?
? ? ? ? ?@Override
? ? ? ? ?public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
? ? ?
? ? ? ? ?}
? ? ?}

然后在spring配置中注入myLocaleResolver

? ? ?@Bean
? ? ?public LocaleResolver localeResolver(){
? ? ? ? ?return new myLocaleResolver();
? ? ?
? ? ?}

 **注意:方法名必須是localeResolver**,**因為源碼中名字為localeResolver的bean**

7. 最后我們來測試一下

而且控制臺輸出也沒問題

總結(jié)

到此這篇關(guān)于springboot頁面國際化配置的文章就介紹到這了,更多相關(guān)springboot頁面國際化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 自己編寫IOC控制反轉(zhuǎn)及AOP面向切面

    自己編寫IOC控制反轉(zhuǎn)及AOP面向切面

    本文展示通過一個案例來自己手寫IOC和AOP代碼,通過銀行轉(zhuǎn)賬案例詳細(xì)的代碼編寫和文檔解釋來說明IOC和AOP的思想,會分享存在的問題和解決問題的思路
    2021-06-06
  • Spring Boot整合Lombok的方法詳解

    Spring Boot整合Lombok的方法詳解

    Lombok 是一種 Java 實用工具,可用來幫助開發(fā)人員消除 Java 的冗長,尤其是對于簡單的 Java 對象(POJO)。這篇文章主要介紹了Spring Boot整合Lombok,需要的朋友可以參考下
    2021-04-04
  • Jmeter測試必知的名詞及環(huán)境搭建

    Jmeter測試必知的名詞及環(huán)境搭建

    我們本章開始學(xué)習(xí)Jmeter,后續(xù)還會有RF以及LoadRunner 的介紹,為什么要學(xué)習(xí)Jmeter,它主要是用來做性能測試的,其中它也需要間接或直接的需要用到抓包工具
    2021-09-09
  • IDEA下SpringBoot指定配置文件啟動項目的全過程

    IDEA下SpringBoot指定配置文件啟動項目的全過程

    我們在使用springboot項目開發(fā)的時候,每次切換環(huán)境跑項目的時候,都得修改配置文件的數(shù)據(jù)庫地址,這樣來回修改感覺很麻煩,這篇文章主要給大家介紹了關(guān)于IDEA下SpringBoot指定配置文件啟動項目的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • 詳解Java拋出和聲明異常的代碼實現(xiàn)

    詳解Java拋出和聲明異常的代碼實現(xiàn)

    我們在編寫代碼時,有時候因為某些原因,并不想在這個方法中立即處理產(chǎn)生的異常,也就是說并不想進行異常的捕獲,接下來小編就來教會大家該如何進行異常的拋出,需要的朋友可以參考下
    2023-08-08
  • java中反射和注解的簡單使用方法

    java中反射和注解的簡單使用方法

    相信大家對注解和反射應(yīng)該并不陌生,在現(xiàn)在信息飛速發(fā)展的年代,各種優(yōu)秀的框架或許都離不開注解的使用,這篇文章主要給大家介紹了關(guān)于java中反射和注解的簡單使用方法,需要的朋友可以參考下
    2021-08-08
  • 基于Socket類以及ServerSocket類的實例講解

    基于Socket類以及ServerSocket類的實例講解

    下面小編就為大家?guī)硪黄赟ocket類以及ServerSocket類的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • K均值聚類算法的Java版實現(xiàn)代碼示例

    K均值聚類算法的Java版實現(xiàn)代碼示例

    這篇文章主要介紹了K均值聚類算法的Java版實現(xiàn)代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • PageHelper插件實現(xiàn)一對多查詢時的分頁問題

    PageHelper插件實現(xiàn)一對多查詢時的分頁問題

    這篇文章主要介紹了PageHelper插件實現(xiàn)一對多查詢時的分頁問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java微信退款開發(fā)

    Java微信退款開發(fā)

    這篇文章主要為大家詳細(xì)介紹了Java微信退款開發(fā)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09

最新評論