溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot頁面國際化如何配置

發(fā)布時間:2022-03-05 13:35:00 來源:億速云 閱讀:208 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了springboot頁面國際化如何配置的相關(guān)知識,內(nèi)容詳細(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目錄下

springboot頁面國際化如何配置

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

還有一種方式也能實(shí)現(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中文

springboot頁面國際化如何配置

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

springboot頁面國際化如何配置

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)備好了,怎樣實(shí)現(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)它實(shí)現(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**,**因?yàn)樵创a中名字為localeResolver的bean**

springboot頁面國際化如何配置

7. 最后我們來測試一下

springboot頁面國際化如何配置

springboot頁面國際化如何配置

springboot頁面國際化如何配置

而且控制臺輸出也沒問題

關(guān)于“springboot頁面國際化如何配置”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“springboot頁面國際化如何配置”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI