一,新建一个springboot项目:和前一篇拦截器交叉,新增的是MyLocalResolver.java
二,文件配置
i18n文件夹:
login.properties:默认中文
login.btn=\u767B\u5F55
login.password=\u5BC6\u7801
login.username=\u7528\u6237\u540D
login_en_US.properties
login.btn=Sign in
login.password=Password
login.username=UserName
login_zh_CN.properties
login.btn=\u767B\u5F55
login.password=\u5BC6\u7801
login.username=\u7528\u6237\u540D
yml配置:
server:
port: 8080
spring:
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
messages:
basename: i18n.login
三,代码书写
前端界面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
input {
line-height: 25px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div style="text-align: center; margin: 20%;">
<form action="toindex" method="post"
style="text-align: center; margin: 20%;">
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
<label th:text="#{login.username}">用户名:</label>
<input type="text" name="username" th:placeholder="#{login.username}">
<br>
<label th:text="#{login.password}">密码:</label>
<input type="password" name="password" th:placeholder="#{login.password}">
<br>
<button type="submit" style="width: 60px; height: 40px;margin-bottom: 20px;" th:text="#{login.btn}">登录</button>
<br>
<a th:href="@{/login.html(l='zh_CN')}">中文</a>
<a th:href="@{/login.html(l='en_US')}">English</a>
</form>
</div>
</body>
</html>
MyLocalResolver.java:
package com.meng.component;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
public class MyLocalResolver implements LocaleResolver{
//以短横杠的形式识别
@Override
public Locale resolveLocale(HttpServletRequest request) {
// TODO Auto-generated method stub
String l = request.getParameter("l");
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(l)) {
String[] split = l.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
// TODO Auto-generated method stub
}
}
MyMvcConfig: 注册国际化组件
package com.meng.config;
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.meng.component.LoginHandlerInterceptor;
import com.meng.component.MyLocalResolver;
@SuppressWarnings("deprecation")
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求来到 success
registry.addViewController("meng").setViewName("login");
}
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册在容器
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
registry.addViewController("/main.html").setViewName("main");
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// super.addInterceptors(registry);
//静态资源不用管,springboot已做好
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/login.html","/","/toindex","/toLogin","/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");
}
};
return adapter;
}
//注册国际化组件
@Bean
public LocaleResolver localeResolver() {
return new MyLocalResolver();
}
}
四,效果展示
英文:
中文: