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. Cesium中级教程2 - 图层

    Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ Cesium支持从几个标准服务绘制和添加高分辨率图像(地图)图层 ...

  2. linux文件编辑器快捷方式

    一:文件编辑器快捷方式 7.光标快速移动快捷方式 ①. 快速切换光标到底行 shift + G ②. 快速切换光标到首行 gg ③. 快速跳转到行首 0 ④. 快速跳转到行尾 shift + $ ⑤. ...

  3. Hello,find!

    Hello,find! 前言 余幼时,每至除夕,爆竹声声,如雷贯耳,醒于梦中,便知春节将至.与兄长二三人,执摔炮六七只, 玩耍于廷中.出门罢,廷中白雪覆于黄土之上.二三尺之外,见犬窝,余之爱犬趴于其中 ...

  4. springboot 的运行原理?

    一.@SpringbootApplicaion 是一个组合注解?  在注解中点击查看. 作用:实现自动配置. /* * springboot的运行原理 1. @SpringbootApplicatio ...

  5. oracle 快速创建用户

    create user  testdb identified by 123456; grant  dba to testdb;

  6. https的页面内嵌入http页面报错的问题

    1.https的页面内嵌入http页面报错 在HTTPS的页面上嵌入http的页面时,浏览器会直接报错.比如在HTTPS页面上用 iframe 直接嵌入一个 http 页面,比如我们可以在百度上直接嵌 ...

  7. Java IO: ByteArrayOutputStream使用

    感谢原文作者:小思思smile 原文链接:https://blog.csdn.net/u014049880/article/details/52329333/ 更多请查阅Java API文档! 在创建 ...

  8. JVM学习五:性能监控工具

    一.系统性能监控 系统性能工具用于确定系统运行的整体状态,基本定位问题所在. Linux – uptime • 系统时间 • 运行时间 n 例子中为7分钟 • 连接数 n 每一个终端算一个连接 • 1 ...

  9. @Resource注解和@Autowired注解

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11770982.html 1. @Resource 类来源: javax(Java扩展包) 类全 ...

  10. FileInputStream 类与 FileReader 类的区别

    FileInputStream 类与 FileReader 类的区别: 两个类的构造函数的形式和参数都是相同的,参数为 File 对象或者表示路径的 String ,它们到底有何区别呢? FileIn ...