springboot页面国际化
- 引入依赖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>
导入网页资源,这里给大家推荐一个我自己在使用的页面资源,SB ADMIN-2
html页面放在templates目录下,这是thymeleaf默认的解析目录,其他的样式文件放在static目录下

接管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";
}
}
- 国际化配置文件:en_US英文,zh_CN中文

点击左上角加号,便可以添加配置的属性,只要在右边填写相应的中英文即可
- 国际化配置文件:en_US英文,zh_CN中文

5. 配置文件已经写好,如何在我们的页面中使用呢?thyme leaf的作用又来了
首先在你的网页添加这样的头部
```html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
```
在所有的html属性前加**th:**就被thymeleaf接管了,根据thymeleaf 语法,获取国际化值使用**#{}**,本地值用**${}**,url用**@{}**


```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页面国际化的更多相关文章
- SpringBoot Web开发(5) 开发页面国际化+登录拦截
SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...
- SpringBoot 国际化配置,SpringBoot Locale 国际化
SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...
- SpringBoot页面访问处理
SpringBoot页面访问处理 1.介绍 Springboot推荐使用thymeleaf模板引擎搭载html页面实现jsp动态渲染效果,因此这里才会用该种方案进行. 2.集成步骤 引入thymele ...
- 配置和修改springboot默认国际化文件
SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...
- SpringBoot的国际化使用
在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用. 在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解 ...
- SpringBoot整合国际化功能
(1).编写国际化配置文件 在resources下新建i18n文件夹,并新建以下文件 ①index.properties username=username ②index_en_US.proper ...
- SpringBoot日记——国际化篇
听起来高大上的国际化,起始就是在利用浏览器语言,或者页面中的中英文切换,将页面的文字在其他语言和中文进行切换,比如: 我们想让这个功能实现,点击中文,页面就是中文的,点击英文就是英文的. 国际化配置 ...
- SpringBoot资源国际化
Springboot根据浏览器实现网站资源国际化 根据浏览器地区主动选择资源 1.创建资源化文件 resource目录下创建messages目录 创建messages_en_US.properties ...
- SpringBoot配置国际化
1).国际化 1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤: 1).编写国 ...
随机推荐
- NAO机器人开发环境配置
python python2.7用于NAO开发 https://www.python.org/downloads/release/python-2718/ python3.6用于其他程序测试. htt ...
- K8S探针和SVC,POD原理
(6)容器是否健康: spec.container.livenessProbe.若不健康,则Pod有可能被重启(可配置策略) (7)容器是否可用: spec.container.readiness ...
- 用socket写一个简单的服务器
import socketsk=socket.socket()sk.bind(("127.0.0.1",7001))sk.listen()def login(url): with ...
- django之mysqlclient安装
如果运行环境中没有安装mysqlclient,在迁移数据库时会发生错误 一.在windows下安装: ·如果直接使用 pip install mysqlclient 会提示安装失败(版本不对或者找不到 ...
- 「JOI 2014 Final」飞天鼠
「JOI 2014 Final」飞天鼠 显然向上爬是没有必要的,除非会下降到地面以下,才提高到刚好为0. 到达一个点有两种情况:到达高度为0和不为0. 对于高度不为0的情况,显然花费的时间越少高度越高 ...
- JS 高阶函数
笔记整理自:廖雪峰老师的JS教程 目录 概述 Array中的高阶函数 map(返回新的Array) reduce(返回新的Array) filter(返回新的Array) sort(返回同一Array ...
- Git提示“warning: LF will be replaced by CRLF”
感谢原文作者:萌新李同学(李俊德-大连理工大学) 原文链接:https://blog.csdn.net/wq6ylg08/article/details/88761581 问题描述 windows平台 ...
- PHP扩展开发编译环境的搭建
PHP允许使用扩展的方式,直接使用C语言开发PHP的功能,由于编译的问题,很多底层和框架的基础工作如果用PHP必然会带来额外的开销,降低系统的吞吐量,必然yaf就是这样的思路,把框架封装在扩展里,让系 ...
- CDN 的缓存与回源机制解析
CDN的缓存与回源机制解析 CDN (Content Delivery Network,即内容分发网络)指的是一组分布在各个地区的服务器.这些服务器存储着数据的副本,因此服务器可以根据哪些服务器与用户 ...
- Ajax接收服务器返回的信息response
Ajax可以向服务器发起请求,有去的方式,那么久必然可疑返回. 服务器返回的信息也可以通过Ajax接收. Ajax共有5种状态: 1.创建对象,没有调用open方法 2.对象发起请求http,已经调用 ...