1.-自定义国际化语言解析器
package com.atguigu.springbootweb.component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;
/**
* 作者:
* 创建时间:2019/6/30
* 项目名字:com.atguigu.springbootweb.component
* 版本:1.0
**/
//发送请求的时候带上语言参数
public class MycoaleResolver implements LocaleResolver {
// 这样切换语言的BUG 在浏览器切换语言不管用了
@Override
public Locale resolveLocale(HttpServletRequest request) {
String l=request.getParameter("l");
//如果参数中不带 l() 参数,就用默认的L,带的话就用参数中的语言信息
Locale locale=Locale.getDefault();
if(!StringUtils.isEmpty(l))
{
String [] split1=l.split("_");
locale=new Locale(split1[0],split1[1]);
HttpSession session = request.getSession();
session.setAttribute(l,locale);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
2.创建配置类,将组件加入容器中去
package com.atguigu.springbootweb.config;
import com.atguigu.springbootweb.component.MycoaleResolver;
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.WebMvcConfigurerAdapter;
/**
* 作者:
* 创建时间:2019/6/30
* 项目名字:com.atguigu.springbootweb.config
* 版本:1.0
**/
//WebMvcConfigurationSupport 来扩展springmvc的功能,但是自动配置失效,继承WebMvcConfigurerAdapter,
// 自动配置不会失效
//@EnableWebMvc//全面接管SpringMvc
@Configuration//此注释是一个配置类
public class MyMvcConfig extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry) {
//意思时浏览器发送硅谷请求也来到success请求(以后一个静态页面的话可以使用这种方式)
registry.addViewController("/guigu").setViewName("success");
}
//将组建注册在容器中(如果咩有bean,会访问public文件夹下的index.html)
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
WebMvcConfigurerAdapter dapter =new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
}
};
return dapter;
}
// 将写好的语言组件加入容器中,才能生效(LocaleResolver)
@Bean(name = "localeResolver")//value时必须的
public LocaleResolver getMycoaleResolver(){
return new MycoaleResolver();
}
}
3.界面参数中加入语言信息
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<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 href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" th:href="@{asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" src="asserts/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" th:text="#{login.userName}" >Username</label>
<input type="text" class="form-control" placeholder="Username" required="" autofocus="">
<label class="sr-only" th:text="#{login.passworld}">Password</label>
<input type="password" class="form-control" placeholder="Password" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit"th:text="#{login.sign}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
</form>
</body>
</html>