样式生效
html是由thyemeleaf来接管,使样式生效,使用th:src="@{/img/bootstrap-solid.svg}"
来代替原先样式。
国际化
1.在资源下创建i18n目录,配置好需要转换得语言配置文件
英文
login.btn=Sign in
login.password=Password
login.remeber=Remerber me
login.tip=Please sign in
login.username=Username
中文
login.btn=登录
login.password=密码
login.remeber=记住我
login.tip=请登录
login.username=用户名
2.语言切换按钮给予参数,根据参数来实现中英文转换。
//对文本内容进行替换
<input type="password" class="form-control" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
//对按钮进行内容替换
<input type="checkbox" value="remember-me" > [[#{login.remeber}]]
</label>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
3.创建自定义国际化组件
public class MyLocaleResolver implements LocaleResolver {
//请求绑定
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
//获取请求中得参数
String language = httpServletRequest.getParameter("l");
//使用默认
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(language)){
String[] split = language.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
4.将组件注册到容器中
@Configuration
public class MyMVCConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
//自定义国际化组件注册
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}