日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring Boot通过url设置国际化

發(fā)布時(shí)間:2025/3/15 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot通过url设置国际化 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

?

?

理論

源碼


?

理論

通過(guò)獲取瀏覽器中g(shù)et方法的參數(shù),構(gòu)造LocaleResolver,從LocaleResolver中調(diào)用對(duì)應(yīng)的properties文件。

在MvcConfig中創(chuàng)建這個(gè)自己定義的LocaleResolver類(lèi)!

程序運(yùn)行截圖如下:

?

源碼

程序結(jié)構(gòu)如下:

MyLocaleResolver.java

package internationdemotest.demo.component;import org.springframework.web.servlet.LocaleResolver; import org.thymeleaf.util.StringUtils;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Locale;public class MyLocaleResolver implements LocaleResolver {@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {String l = httpServletRequest.getParameter("l");Locale locale = Locale.getDefault();if(!StringUtils.isEmpty(l)){String[] split = l.split("_");locale = new Locale(split[0], split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {} }

MyMvcConfig.java

package internationdemotest.demo.config;import internationdemotest.demo.component.MyLocaleResolver; 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.WebMvcConfigurerAdapter;@Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter{@Beanpublic WebMvcConfigurerAdapter webMvcConfigurerAdapter(){WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("index.html").setViewName("index");}};return adapter;}@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();} }

index.properties

index.hehe=呵呵#

index_en_US.properties

index.hehe=hehehehehehhehehehehe

index_zh_CN.properties

index.hehe=呵呵

index.html

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1 th:text="#{index.hehe}">呵呵</h1><a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a> <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a></body> </html>

application.properties

spring.messages.basename=i18n.index

?

總結(jié)

以上是生活随笔為你收集整理的Spring Boot通过url设置国际化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。