php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 519|回复: 0

springboot页面国际化

[复制链接]

2645

主题

2652

帖子

9404

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6637
贡献
0
注册时间
2021-4-14
最后登录
2024-5-3
在线时间
670 小时
QQ
发表于 2022-3-3 19:38:29 | 显示全部楼层 |阅读模式
引入依赖pom.xml
[mw_shl_code=java,true]                <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>

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

html页面放在templates目录下,这是thymeleaf默认的解析目录,其他的样式文件放在static目录下
image.png
接管spring Mvc,自定义url访问路径,可做可不做

建一个config目录,在这里建一个myWebMvcConfig
[mw_shl_code=java,true]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
    }
}
[/mw_shl_code]
路径可以设置多个,这样只要是这三个url,spring 都会访问register.html
还有一种方式也能实现
[mw_shl_code=java,true]import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class demoController {
    @RequestMapping({"/","/wq"})
    public String test(){
        return "register";
    }
}
[/mw_shl_code]
国际化配置文件:en_US英文,zh_CN中文
image.png
点击左上角加号,便可以添加配置的属性,只要在右边填写相应的中英文即可
image.png .
配置文件已经写好,如何在我们的页面中使用呢?thyme leaf的作用又来了
[mw_shl_code=applescript,true]  首先在你的网页添加这样的头部

  ```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**
[/mw_shl_code]
image.png
最后我们来测试一下
image.png
image.png
image.png





上一篇:记一次阿里云oss文件上传服务假死
下一篇:用 UI 多线程处理 WPF 大量渲染的解决方案
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-5-3 11:00 , Processed in 0.184441 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表