springboot中页面国际化

页面国际化

目的:通过按钮如何在同一页面切换,请求会变,页面自然会属性,而只有一个html页面,但是所含内容语言发生改变.

点击中文

在这里插入图片描述

点击English

在这里插入图片描述
步骤开始:

1.配置i18n

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.编写MyLocaleResolver,解析html页面发来的请求

package com.lianwei.kuang.config;

import org.apache.tomcat.jni.Local;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocaleResolver implements LocaleResolver {
    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //获取请求中的语言参数
        String language =httpServletRequest.getParameter("1");
        System.out.println("Debug===>"+language);
        Locale locale = Locale.getDefault();//如果没有就使用默认的
       //如果请求的链接携带了国际化的参数
        if(!StringUtils.isEmpty(language)){
         //zh_CN
         String[] split = language.split("_");
         //国家,地区
            locale = new Locale(split[0],split[1]);
            System.out.println("split:"+split);
            System.out.println("local:"+locale);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

3.首页以及使用@Bean将写得国际化组件生效

package com.lianwei.kuang.config;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class Default implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController( "/index.html" ).setViewName( "index" );
       // registry.setOrder( Ordered.HIGHEST_PRECEDENCE );
    }
    //自定义的国际化组件就生效了
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

4.html页面

<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<html lang="en">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <meta name="description" content="">
      <meta name="author" content="">
      <title>Signin Template for Bootstrap</title>
      <!-- Bootstrap core CSS -->
      <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
      <!-- Custom styles for this template -->
      <link th:href="@{/css/signin.css}" rel="stylesheet">
   </head>

   <body class="text-center">
      <form class="form-signin" action="dashboard.html">
         <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
         <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
         <label class="sr-only">Username</label>
         <input type="text" class="form-control" th:placeholder="#{login.Username}" required="" autofocus="">
         <label class="sr-only">Password</label>
         <input type="password" class="form-control" th:placeholder="#{login.password}" required="">
         <div class="checkbox mb-3">
            <label>
          <input type="checkbox" value="remberber-me">[[#{login.remember}]]
        </label>
         </div>
         <button class="btn btn-lg btn-primary btn-block"  th:text="#{login.btn}" type="submit">Sign in</button>
         <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
         <a class="btn btn-sm" th:href="@{/index.html(1='zh_CN')}">中文</a>
         <a class="btn btn-sm" th:href="@{/index.html(1='en_US')}">English</a>
      </form>

   </body>

</html>

总结:

1.首页配置:

<1>.所有的页面的静态资源都需要使用thymeleaf接管;

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

​ <2>.@{}

2.页面国际化:

​ <1>.配置i18n文件

​ <2>.需要在项目中进行按钮自动切换,自定义组件LocaleResolver

​ <3>.使用@Bean将自己写的组件配置到spring容器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值