springboot頁(yè)面國(guó)際化配置指南
前言
前一段時(shí)間做了一個(gè)項(xiàng)目,需要解決中文、繁體、英文的國(guó)際化問(wèn)題,所以本文將詳細(xì)介紹springboot頁(yè)面國(guó)際化配置的過(guò)程
方法如下
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)頁(yè)資源,這里給大家推薦一個(gè)我自己在使用的頁(yè)面資源,SB ADMIN-2
html頁(yè)面放在templates目錄下,這是thymeleaf默認(rèn)的解析目錄,其他的樣式文件放在static目錄下

3.接管spring Mvc,自定義url訪問(wèn)路徑,可做可不做
建一個(gè)config目錄,在這里建一個(gè)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è)置多個(gè),這樣只要是這三個(gè)url,spring 都會(huì)訪問(wèn)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.國(guó)際化配置文件:en_US英文,zh_CN中文

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

5. 配置文件已經(jīng)寫好,如何在我們的頁(yè)面中使用呢?thyme leaf的作用又來(lái)了
首先在你的網(wǎng)頁(yè)添加這樣的頭部
<html lang="en" xmlns:th="http://www.thymeleaf.org">
在所有的html屬性前加**th:**就被thymeleaf接管了,根據(jù)thymeleaf 語(yǔ)法,獲取國(guó)際化值使用**#{}**,本地值用**${}**,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. 頁(yè)面和配置文件都準(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;
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ?}我們?cè)僬业紸cceptHeaderLocaleResolver.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**

7. 最后我們來(lái)測(cè)試一下



而且控制臺(tái)輸出也沒(méi)問(wèn)題
總結(jié)
到此這篇關(guān)于springboot頁(yè)面國(guó)際化配置的文章就介紹到這了,更多相關(guān)springboot頁(yè)面國(guó)際化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot微服務(wù)項(xiàng)目集成html頁(yè)面的實(shí)現(xiàn)
本文主要介紹了springboot微服務(wù)項(xiàng)目集成html頁(yè)面的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Jackson忽略字段實(shí)現(xiàn)對(duì)字段進(jìn)行序列化和反序列化
在使用?Jackson?進(jìn)行序列化和反序列化時(shí),有時(shí)候需要對(duì)某些字段進(jìn)行過(guò)濾,以便在?JSON?數(shù)據(jù)中不包含某些敏感信息,下面就一起來(lái)了解一下Jackson忽略字段實(shí)現(xiàn)對(duì)字段進(jìn)行序列化和反序2023-10-10
淺談PrintStream和PrintWriter的區(qū)別和聯(lián)系
這篇文章主要介紹了淺談PrintStream和PrintWriter的區(qū)別和聯(lián)系,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Java實(shí)現(xiàn)讀取和寫入properties文件
這篇文章主要介紹了Java實(shí)現(xiàn)讀取和寫入properties文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
java并發(fā)學(xué)習(xí)之Executor源碼解析
這篇文章主要為大家介紹了java并發(fā)學(xué)習(xí)之Executor源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
解析Spring Boot 如何讓你的 bean 在其他 bean&n
在 SpringBoot 中如何讓自己的某個(gè)指定的 Bean 在其他 Bean 前完成被 Spring 加載?我聽到這個(gè)問(wèn)題的第一反應(yīng)是,為什么會(huì)有這樣奇怪的需求?下面小編給大家分析下Spring Boot 如何讓你的 bean 在其他 bean 之前完成加載 ,感興趣的朋友一起看看吧2024-01-01
Spring Native實(shí)現(xiàn)0.059s啟動(dòng)一個(gè)SpringBoot項(xiàng)目
Spring Native是Spring框架的一個(gè)子項(xiàng)目,旨在提供一種將Spring應(yīng)用程序編譯為本地可執(zhí)行文件的方法,從而提高啟動(dòng)時(shí)間和資源效率,本文主要介紹了Spring Native實(shí)現(xiàn)0.059s啟動(dòng)一個(gè)SpringBoot項(xiàng)目,感興趣的可以了解一下2024-02-02
從字節(jié)碼角度解析synchronized和反射實(shí)現(xiàn)原理
這篇文章主要介紹了從字節(jié)碼角度解析synchronized和反射的實(shí)現(xiàn)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

