1、编写国际化配置文件,抽取页面需要显示的国际化消息

(1)在resources文件夹下创建i18n文件夹,用来存放国际化配置文件,为什么是i18n???

i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称。

然后任意打开一个配置文件,如login_en_US.properties,点击Resources Bundle,输入你想要国际化的属性

(2)在全局配置文件中绑定这些自定义的国际化配置
SpringBoot自动配置好了管理国际化资源文件的组件

@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {/*** Comma-separated list of basenames (essentially a fully-qualified classpath* location), each following the ResourceBundle convention with relaxed support for* slash based locations. If it doesn't contain a package qualifier (such as* "org.mypackage"), it will be resolved from the classpath root.*/private String basename = "messages";  //我们的配置文件可以直接放在类路径下叫messages.properties;@Beanpublic MessageSource messageSource() {ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();if (StringUtils.hasText(this.basename)) {//设置国际化资源文件的基础名(去掉语言国家代码的)messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(this.basename)));}if (this.encoding != null) {messageSource.setDefaultEncoding(this.encoding.name());}messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);messageSource.setCacheSeconds(this.cacheSeconds);messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);return messageSource;}

我们的配置文件可以直接放在类路径下叫messages.properties 不用做任何配置就可使用国际化,但我们这里创建了一个i18n文件夹,所以我们要在全局配置文件中通过spring.messages.messages绑定资源文件的基础名(去掉语言国家代码的)

application.properties

spring.messages.basename=i18n.login

2、修改login.html

https://www.webjars.org/
公共资源用webjars提供的东西,自己的资源就用assets文件夹里的

<!--加入Bootstrap依赖--><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>4.5.0</version></dependency>

login.html

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Signin Template for Bootstrap</title><!-- Bootstrap core CSS --><link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.5.0/css/bootstrap.css}" rel="stylesheet"><!-- Custom styles for this template --><link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head><body class="text-center">
<form class="form-signin" action="dashboard.html"><img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1><label class="sr-only" th:text="#{login.username}">Username</label><input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus=""><label class="sr-only" th:text="#{login.password}">Password</label><input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required=""><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me"/> [[#{login.remember}]]</label></div><button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button><p class="mt-5 mb-3 text-muted">© 2017-2018</p><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>
</form>
</body>
</html>

3、编写MyLocaleResolver实现LocaleResolver接口

(1)创建MyLocaleResolver类

//可以在连接上携带区域信息
public class MyLocaleResolver implements LocaleResolver {@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {String l = httpServletRequest.getParameter("l");// 如果没有 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) {}
}

为什么要实现LocaleResolver接口呢??
国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);

         @Bean@ConditionalOnMissingBean@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")public LocaleResolver localeResolver() {if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {return new FixedLocaleResolver(this.mvcProperties.getLocale());}AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();localeResolver.setDefaultLocale(this.mvcProperties.getLocale());return localeResolver;}
默认的就是根据请求头带来的区域信息获取Locale进行国际化

(2)在配置类中注册这个Bean

//使用WebMvcConfigurer可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {//所有的WebMvcConfigurer组件都会一起起作用@Bean //将组件注册在容器public WebMvcConfigurer webMvcConfigurer(){WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");registry.addViewController("/index.html").setViewName("login");}};return webMvcConfigurer;}@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}
}

ok,到登录页面按中/英文按钮可以切换成中英文页面

15 | SpringBoot + thymeleaf 的登陆页面如何实现国际化相关推荐

  1. 2021-2027全球与中国登陆页面创建软件市场现状及未来发展趋势

    本文研究全球及中国市场登陆页面创建软件现状及未来发展趋势,侧重分析全球及中国市场的主要企业,同时对比北美.欧洲.日本.中国.东南亚.印度等地区的现状及未来发展趋势. 2019年全球登陆页面创建软件市场 ...

  2. 2021-2027全球与中国登陆页面生成软件市场现状及未来发展趋势

    本文研究全球及中国市场登陆页面生成软件现状及未来发展趋势,侧重分析全球及中国市场的主要企业,同时对比北美.欧洲.日本.中国.东南亚.印度等地区的现状及未来发展趋势. 2019年全球登陆页面生成软件市场 ...

  3. Ajax+SpringBoot+Thymeleaf使用中遇到的跳转页面问题

    前言:这周在使用 Ajax+Thymeleaf 时遇到一个问题,折腾了我很久,在此记录一下 Ajax+SpringBoot+Thymeleaf使用中遇到的跳转页面问题 问题描述 我的目的:通过 Aja ...

  4. SpringBoot Thymeleaf使用教程(实用版)

    SpringBoot Thymeleaf使用教程(实用版) 使用Thymeleaf 三大理由: 简洁漂亮 容易理解 完美支持HTML5 使用浏览器直接打开页面 不新增标签 只需增强属性 学习目标 快速 ...

  5. 基于springboot+thymeleaf+mybatis的员工管理系统 —— 登录与注册

    员工管理系统 - 登录与注册功能 创建项目 pom.xml 数据库表设计和环境准备 建表SQL application.properties 用户注册与登录功能 entity dao service ...

  6. 一篇文章解决springboot+thymeleaf多语言国际化

    1.前言 博主最近在写一个多语言的项目,因为之前没实际接触过多语言的设计,所以写这篇文章记录下这次多语言开发的过程. 博主的开发环境是:Springboot1.5.6 + thymeleaf,需要注意 ...

  7. SpringBoot + thymeleaf + mysql + html<video> 实现读取视频列表并播放视频

    SpringBoot + thymeleaf + mysql + html 实现读取视频列表并播放视频 通过读取数据库video表获取当前视频的视频名.视频地址,展示至前端页面videorecord. ...

  8. 基于javaweb的汉服文化bbs商城系统(java+springboot+thymeleaf+html+layui+bootstrap+mysql)

    基于javaweb的汉服文化bbs商城系统(java+springboot+thymeleaf+html+layui+bootstrap+mysql) 运行环境 Java≥8.MySQL≥5.7 开发 ...

  9. SpringSecurity自定义登陆页面和跳转页面

    如果我们不用form-login说明登陆界面,springsecurity框架将自动为我们生成登陆界面 现在我们不想用自动生成的登陆界面了,而想使用自定义的漂亮的登陆界面 则需要使用<secur ...

最新文章

  1. 好想自己做个迷宫呀!
  2. 性能超过人类炼丹师,AutoGluon 低调开源
  3. 重温ES6核心概念和基本用法
  4. Apache Flink 漫谈系列 - JOIN 算子
  5. 计算机硬件的组装实践,毕业论文-计算机硬件组装实践.doc
  6. VMWare共享文件
  7. 《2018年云上挖矿态势分析报告》发布,非Web类应用安全风险需重点关注
  8. Oracle 索引概述
  9. oracle 布尔转换java布尔_java 布尔值一种赋值方法
  10. 当心XML文件中的非法字符
  11. 10个问题让你快速避开java中的jdbc常见坑
  12. Spring_对缓存的支持
  13. c++ 遍历所有点且距离最短_图解:最短路径之迪杰斯特拉算法
  14. 解决win7检测不到第二个显示器的方法
  15. java的编程规范_JAVA编程规范-OOP规范
  16. 大数据在企业中的运用
  17. Android xml manifest属性详解
  18. 如何利用迅雷下载百度云获取实际下载地址
  19. 计算机课堂小游戏活跃气氛,适合小学生在课堂上玩的游戏,简单有趣活跃课堂气氛...
  20. VB调用摄像头录像,拍照,保存

热门文章

  1. 基于BSC测试网收益聚合器Beefy协议的编译、测试、部署
  2. Cmake学习实战-基础篇
  3. android手机号码恢复,手把手教你如何恢复安卓手机通讯录里面丢失的数据
  4. 图书销售系统(C#界面设计)
  5. 2022-12-16 leetcode与蓝桥刷题情况
  6. 用canvas画布绘制扇形和弧线
  7. 计算机专业武汉的就业方向,计算机专业好就业吗
  8. 祖传代码如何优化性能?
  9. java线程(Thread)的创建与常用方法
  10. 基于ssm的药房药店药品管理系统