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. 解决new Thread().Start导致高并发CPU 100%的问题

    背景 之前接手一个项目的时候,发现到处是 new Thread(()=>{ //do something }).Start(); 这么做的目的,无非是为了减少页面等待时间提高用户体验,把一些浪费 ...

  2. gin框架中的会话控制

    Cookie介绍 Http协议是无状态的,服务器不能记录浏览器的访问状态,也就是说服务器不能判断请求的客户端是否已经登录 Cookie就是解决http协议无状态的方案之一 Cookie实际上就是服务器 ...

  3. 前端 | Vue nextTick 获取更新后的 DOM

    前两天在开发时遇到一个需求:打开对话框的时候自动聚焦其中的输入框.由于原生的 autofocus 属性不起作用,需要使用组件库提供的 focus 方法手动手动获取焦点.于是有如下代码: <el- ...

  4. openstack,docker,mesos,Kubernetes(k8s)

    作者:张乾链接:https://www.zhihu.com/question/62985699/answer/204233732来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  5. 如何在 pyqt 中解决国际化 tr() 函数不起作用的问题

    前言 有些时候我们在父类中使用了 self.tr('XXX'),使用 Qt Linguist 完成翻译并导出 qm 文件后,发现子类中仍然是英文原文.比如下面这段代码: class AlbumCard ...

  6. 解决 413 Request Entity Too Large

    修改配置文件  vim /etc/nginx/sites-available/default,增加 client_max_body_size 1000m;//最大上传大小 proxy_connect_ ...

  7. NSURLConnection和Runloop(面试)

    (1)两种为NSURLConnection设置代理方式的区别 //第一种设置方式: //通过该方法设置代理,会自动的发送请求 // [[NSURLConnection alloc]initWithRe ...

  8. (全局npmrc)nrm、npmrc、package-lock.json 的优先级

    npmrc 测试 nrm.npmrc 的优先级 实验 1. 没有设置 nrm. 默认设置 registry 为 https://registry.npmjs.org/ 下载的所有包都是通过以上域名获取 ...

  9. 学习Java Web篇:MVC设计模式

    一.MVC设计模式 1.什么是MVC模式 1.1.MVC -- Model View Controller模型视图控制器 1.2.Model:模型 一个功能 一般用JavaBean 1.3.View: ...

  10. 【CF712E】Memory and Casinos(数学 期望 DP)

    题目链接 大意 给出一个序列,当你在某个点时,有一个向右走的概率\(P_i\)(向左为\(1-P_i\)), 给出\(M\)个操作,操作有两类: 1 X Y Z:把\(P_X\)的值修改为\(\fra ...