如果我的博客能够帮到大家能够点个赞,关注一下,以后还会更新更过JavaWeb的高级技术,大家的支持就是我继续更新的动力。谢谢咯。
- WebMvcConfiguration.java 配置类
package com.nyist.springboot04web.config;
import com.nyist.springboot04web.Resolver.MyLocaleResolver;
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;
//使用WebMvcConfigurationAdapter 作为配置文件
//@EnableWebMvc 不要接管SpringMVC
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/nyist").setViewName("view");
}
//所有WebMvcConfigurationAdapter的组件会一起起作用
//前提是 SpringBoot容器必须知道 该组件的存在
@Bean
public WebMvcConfigurerAdapter WebMvcConfigurerAdapter() {
WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
};
return webMvcConfigurerAdapter;
}
//将 该组件中 加入到容器中 localeResolver
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
- MyLocaleResovler.java
package com.nyist.springboot04web.Resolver;
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;
/**
* 可以在连接上携带区域信息,我们需要将我们自己的LocaleResolver 放在容器中去 这样在容器才能配置
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String language = request.getParameter("language");
//定义一个区域对象 如果带区域信息了 就用自己的区域信息 如果没有带区域信息就使用 默认的
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(language)) {
//使用字符分割 语言_国家
String[] s = language.split("_");
locale = new Locale(s[0],s[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { }
}
- login.properties、login_en_US.properties、login_zh_CN.properties
#login_zh_CN
login.btn=登陆
login.password=密码
login.rememberme=记住我
login.tip=请登陆
login.username=用户名
#login_en_US
login.btn=Sign in
login.password=password
login.rememberme=Remebber me
login.tip=Please sign in
login.username=UserName
#login.properties
login.btn=登陆~
login.password=密码~
login.rememberme=请记住我~
login.tip=请登陆~
login.username=用户名~
- index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<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.1.3/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" th:placeholder="#{login.username}" placeholder="" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" th:placeholder="#{login.password}" placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"/> [[#{login.rememberme}]]
</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">© 2018-2019</p>
<a class="btn btn-sm" th:href="@{/(language='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/(language='en_US')}">English</a>
</form>
</body>
</html>
- 运行效果
资源下载:下载