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>
  1. 导入网页资源,这里给大家推荐一个我自己在使用的页面资源,SB ADMIN-2

    html页面放在templates目录下,这是thymeleaf默认的解析目录,其他的样式文件放在static目录下

  2. 接管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
    }
    }

    路径可以设置多个,这样只要是这三个url,spring 都会访问register.html

    还有一种方式也能实现

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping; @Controller
    public class demoController {
    @RequestMapping({"/","/wq"})
    public String test(){
    return "register";
    }
    }
    1. 国际化配置文件:en_US英文,zh_CN中文



      点击左上角加号,便可以添加配置的属性,只要在右边填写相应的中英文即可



5. 配置文件已经写好,如何在我们的页面中使用呢?thyme leaf的作用又来了

  首先在你的网页添加这样的头部

  ```html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
``` 在所有的html属性前加**th:**就被thymeleaf接管了,根据thymeleaf 语法,获取国际化值使用**#{}**,本地值用**${}**,url用**@{}** ![image-20220228101634725](C:\Users\32944\AppData\Roaming\Typora\typora-user-images\image-20220228101634725.png) ![image-20220228101806743](C:\Users\32944\AppData\Roaming\Typora\typora-user-images\image-20220228101806743.png) ```html
<a th:href="@{/register.html(l='zh_CN')}" >中文 </a>
<a th:href="@{/register.html(l='en_US')}">English </a>
``` 6. 页面和配置文件都准备好了,怎样实现跳转呢? 在WebMvcAutoConfiguration.class中 ```java
@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,发现它实现了LocaleResolver ```java
public class AcceptHeaderLocaleResolver implements LocaleResolver {
private final List<Locale> supportedLocales = new ArrayList(4);
@Nullable
private Locale defaultLocale;
``` 那我们就编写自己的LocaleResolver ```java
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 ```java
@Bean
public LocaleResolver localeResolver(){
return new myLocaleResolver(); }
``` **注意:方法名必须是localeResolver**,**因为源码中名字为localeResolver的bean**



7. 最后我们来测试一下







而且控制台输出也没问题

springboot页面国际化的更多相关文章

  1. SpringBoot Web开发(5) 开发页面国际化+登录拦截

    SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...

  2. SpringBoot 国际化配置,SpringBoot Locale 国际化

    SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...

  3. SpringBoot页面访问处理

    SpringBoot页面访问处理 1.介绍 Springboot推荐使用thymeleaf模板引擎搭载html页面实现jsp动态渲染效果,因此这里才会用该种方案进行. 2.集成步骤 引入thymele ...

  4. 配置和修改springboot默认国际化文件

    SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...

  5. SpringBoot的国际化使用

    在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用. 在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解 ...

  6. SpringBoot整合国际化功能

    (1).编写国际化配置文件 在resources下新建i18n文件夹,并新建以下文件 ①index.properties   username=username ②index_en_US.proper ...

  7. SpringBoot日记——国际化篇

    听起来高大上的国际化,起始就是在利用浏览器语言,或者页面中的中英文切换,将页面的文字在其他语言和中文进行切换,比如: 我们想让这个功能实现,点击中文,页面就是中文的,点击英文就是英文的. 国际化配置 ...

  8. SpringBoot资源国际化

    Springboot根据浏览器实现网站资源国际化 根据浏览器地区主动选择资源 1.创建资源化文件 resource目录下创建messages目录 创建messages_en_US.properties ...

  9. SpringBoot配置国际化

    1).国际化 1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤: 1).编写国 ...

随机推荐

  1. 前端3D引擎-Cesium自定义动态材质

    本文代码基于Vue-cli4和使用WebGL的地图引擎Cesium,主要内容为三维场景下不同对象的动态材质构建. 参考了很多文章,链接附在文末. 为不同的几何对象添加动态材质 不知道这一小节的名称概况 ...

  2. zookeeper,kafka,redis等分布式框架的主从同步策略

    1 zookeeper选主机制 1.1 LeaderElection选举算法 选举线程由当前Server发起选举的线程担任,他主要的功能对投票结果进行统计,并选出推荐的Server.选举线程首先向所有 ...

  3. 第01讲:Flink 的应用场景和架构模型

    你好,欢迎来到第 01 课时,本课时我们主要介绍 Flink 的应用场景和架构模型. 实时计算最好的时代 在过去的十年里,面向数据时代的实时计算技术接踵而至.从我们最初认识的 Storm,再到 Spa ...

  4. JavaScript实现禁止打开控制台

    通过 JavaScript 实现禁止打开控制台(期中包括:右键审查元素.工具栏.F12.Shift+Ctrl+I) <!DOCTYPE html> <html lang=" ...

  5. 被mybatis一级缓存坑了

    目录 背景 场景 原因 解法 参考 背景 项目中出现了这样一个问题,就是select出来的数据和数据库里的数据不一样,就非常的奇怪,发现原来是mybatis的缓存导致的,经过查询资料发现这是mybat ...

  6. git命令行-新建分支与已提交分支合并

    例如要将A分支的一个commit合并到B分支: 首先切换到A分支 git checkout A git log 找出要合并的commit ID : 例如 325d41 然后切换到B分支上 git ch ...

  7. Centos设置网络(固定IP)

    简介 设置为桥接模式,即将虚拟机的虚拟网络适配器与主机的物理网络适配器进行交接,虚拟机中的虚拟网络适配器可通过主机中的物理网络适配器直接访问到外部网络. 配置 虚拟机设置为桥接模式 进入网络配置文件, ...

  8. swwager的使用

    最近弄swwager文档,被搞得恼火,故记录一下 先展示一下现有的页面,此页面由swwager自动生成 配置步骤: 一:导入swwager的依赖 <!-- =================== ...

  9. git rebase git merge

    Git rebase 使用方法 1. git checkout feature 2. git rebase master feature 相当于git rebase master + git chec ...

  10. node.js 使用domain模块捕获异步回调中的异常

    和其他服务器端语言相比,貌似node.js 对于异常捕捉确实非常困难. 首先你会想到try/catch ,但是在使用过程中我们会发现并没有真正将错误控制在try/catch 语句中. 为什么? 答案是 ...