springboot页面国际化配置指南

作者:阿落小世界 时间:2022-06-16 14:26:25 

前言

前一段时间做了一个项目,需要解决中文、繁体、英文的国际化问题,所以本文将详细介绍springboot页面国际化配置的过程

方法如下

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>

2.导入网页资源,这里给大家推荐一个我自己在使用的页面资源,SB ADMIN-2

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

springboot页面国际化配置指南

3.接管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";
   }
}

4.国际化配置文件:en_US英文,zh_CN中文

springboot页面国际化配置指南

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

springboot页面国际化配置指南

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

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

<html lang="en" xmlns:th="http://www.thymeleaf.org">

在所有的html属性前加**th:**就被thymeleaf接管了,根据thymeleaf 语法,获取国际化值使用**#{}**,本地值用**${}**,url用**@{}**

? ?<a ?th:href="@{/register.html(l='zh_CN')}" rel="external nofollow" ?>中文 </a>
? <a ?th:href="@{/register.html(l='en_US')}" rel="external nofollow" >English </a>

6. 页面和配置文件都准备好了,怎样实现跳转呢?

 在WebMvcAutoConfiguration.class中

? ? ? ?? ??? ?@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 

? ? ?public class AcceptHeaderLocaleResolver implements LocaleResolver {
? ? ? ? ?private final List<Locale> supportedLocales = new ArrayList(4);
? ? ? ? ?@Nullable
? ? ? ? ?private Locale defaultLocale;

那我们就编写自己的LocaleResolver 

? ? ?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

? ? ?@Bean
? ? ?public LocaleResolver localeResolver(){
? ? ? ? ?return new myLocaleResolver();
? ? ?
? ? ?}

 **注意:方法名必须是localeResolver**,**因为源码中名字为localeResolver的bean**

springboot页面国际化配置指南

7. 最后我们来测试一下

springboot页面国际化配置指南

springboot页面国际化配置指南

springboot页面国际化配置指南

而且控制台输出也没问题

来源:https://www.cnblogs.com/littleworld/p/15961378.html

标签:springboot,国际化,页面
0
投稿

猜你喜欢

  • Android 通过代码安装 APK的方法详解

    2022-11-12 14:37:26
  • Android使用SharedPreferences存储数据的实现方法

    2021-08-22 02:05:49
  • 使用java技术抓取网站上彩票双色球信息详解

    2022-01-04 14:47:51
  • 浅谈Java中注解Annotation的定义、使用、解析

    2021-07-29 04:07:52
  • Java设计模式中的桥接模式

    2023-11-10 08:42:25
  • 详解三种C#实现数组反转方式

    2023-10-09 20:29:23
  • 以Java代码的方式总结几个典型的内存溢出案例

    2023-06-11 04:00:56
  • Java之Spring注解开发案例详解

    2022-05-23 05:33:02
  • Flutter实现手势识别功能详解方法

    2022-12-21 15:41:41
  • Java——对象初始化顺序使用详解

    2023-11-01 01:08:46
  • Java毕业设计实战之生活旅行分享平台的实现

    2022-02-01 02:42:18
  • java使用@Scheduled注解执行定时任务

    2021-09-16 08:37:16
  • JAVA JDK8 List分组获取第一个元素的方法

    2021-06-24 13:07:51
  • Java求解二叉树的最近公共祖先实例代码

    2023-09-14 18:24:56
  • android 添加按(power键)电源键结束通话(挂断电话)

    2023-08-14 20:38:24
  • 彻底掌握C语言strcpy函数的用法

    2023-07-03 07:21:18
  • 如何在Spring Boot应用中优雅的使用Date和LocalDateTime的教程详解

    2023-03-14 04:54:11
  • 浅谈Maven镜像更换为阿里云中央仓库(精)

    2022-08-06 04:48:17
  • C#时间戳基本用法实例分析

    2022-10-12 14:22:40
  • java使用ftp上传文件示例分享

    2021-10-23 08:33:03
  • asp之家 软件编程 m.aspxhome.com