博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
员工管理系统二:首页和国际化实现
阅读量:3966 次
发布时间:2019-05-24

本文共 6364 字,大约阅读时间需要 21 分钟。

首页实现

index.html

			
Signin Template for Bootstrap

方式一:写一个controller实现!

@Controllerpublic class indexController {
// 会解析到templates目录下的index.html页面 @RequestMapping({
"/","/index.html"}) public String index(){
return "index"; }}

方式二:自己编写MVC的扩展配置

package com.loey.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.EnableWebMvc;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * 自己编写扩展springMvc的扩展配置 **/@Configurationpublic class myMvcConfig implements WebMvcConfigurer {
@Override public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index"); registry.addViewController("index.html").setViewName("index"); }}

解决了首页问题,我们还需要解决一个资源导入的问题;

为了保证资源导入稳定,我们建议在所有资源导入时候使用 th:去替换原有的资源路径!这也是模板规范

例如:

		

页面国际化

有的时候,我们的网站会去涉及中英文甚至多语言的切换,这时候我们就需要学习国际化了!

准备工作

先在IDEA中统一设置properties的编码问题!

在这里插入图片描述
编写国际化配置文件,抽取页面需要显示的国际化页面消息。我们可以去登录页面查看一下,哪些内容 我们需要编写国际化的配置!
在这里插入图片描述

配置文件编写

1、我们在resources资源文件下新建一个i18n目录,存放国际化配置文件

2、建立一个login.properties文件,还有一个login_zh_CN.properties;发现IDEA自动识别了我们要做 国际化操作;文件夹变了!
在这里插入图片描述
3、我们可以在这上面去新建一个文件;
在这里插入图片描述
弹出如下页面:我们再添加一个英文的
在这里插入图片描述
这样就快捷多了!
在这里插入图片描述
4、接下来,我们就来编写配置,我们可以看到idea下面有另外一个视图;
在这里插入图片描述
这个视图我们点击 + 号就可以直接添加属性了;我们新建一个login.tip,可以看到边上有三个文件框可 以输入
在这里插入图片描述
然后依次添加其他页面内容即可!
在这里插入图片描述
然后去查看我们的配置文件;
login.properties : 默认

login.pwd=密码login.remember=记住我login.sign=登录login.tip=请登录login.user=用户名

login_en_US.properties

login.pwd=Passwordlogin.remember=Remember melogin.sign=Sign inlogin.tip=Please sign inlogin.user=Username

login_zh_CN.properties

login.pwd=密码login.remember=记住我login.sign=登录login.tip=请登录login.user=用户名

OK,配置文件步骤搞定!

配置文件生效探究

我们去看一下SpringBoot对国际化的自动配置!这里又涉及到一个类:MessageSourceAutoConfiguration

里面有一个方法,这里发现SpringBoot已经自动配置好了管理我们国际化资源文件的组件 ResourceBundleMessageSource

// 获取 properties 传递过来的值进行判断@Beanpublic MessageSource messageSource(MessageSourceProperties properties) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); if (StringUtils.hasText(properties.getBasename())) {
// 设置国际化文件的基础名(去掉语言国家代码的) messageSource.setBasenames(StringUtils .commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename()))); } if (properties.getEncoding() != null) {
messageSource.setDefaultEncoding(properties.getEncoding().name()); } messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale()); Duration cacheDuration = properties.getCacheDuration(); if (cacheDuration != null) {
messageSource.setCacheMillis(cacheDuration.toMillis()); } messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat()); messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage()); return messageSource;}

我们真实 的情况是放在了i18n目录下,所以我们要去配置这个messages的路径;

application.properties

#真实文件放置位置spring.messages.basename=i18n.login

配置页面国际化值

去页面获取国际化的值,查看Thymeleaf的文档,找到message取值操作为:#{…}。我们去页面测试 下:
在这里插入图片描述
IDEA还有提示,非常智能的!
在这里插入图片描述
我们可以去启动项目,访问一下,发现已经自动识别为中文的了!
在这里插入图片描述

但是我们想要更好!可以根据按钮自动切换中文英文!

配置国际化解析

在Spring中有一个国际化的Locale (区域信息对象);里面有一个叫做LocaleResolver (获取区域信息 对象) 的解析器!

我们去webmvc自动配置文件(WebMvcAutoConfiguration),寻找一下!看到SpringBoot默认配置:

@Bean@ConditionalOnMissingBean@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")public LocaleResolver localeResolver() {
// 容器中没有就自己配,有的话就用用户配置的 if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale()); } // 接收头国际化分解 AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver(); localeResolver.setDefaultLocale(this.mvcProperties.getLocale()); return localeResolver;}

AcceptHeaderLocaleResolver 这个类中有一个方法

@Overridepublic Locale resolveLocale(HttpServletRequest request) {
Locale defaultLocale = getDefaultLocale(); // 默认的就是根据请求头带来的区域信息获取Locale进行国际 if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
return defaultLocale; } Locale requestLocale = request.getLocale(); List
supportedLocales = getSupportedLocales(); if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
return requestLocale; } Locale supportedLocale = findSupportedLocale(request, supportedLocales); if (supportedLocale != null) {
return supportedLocale; } return (defaultLocale != null ? defaultLocale : requestLocale);}

那假如我们现在想点击链接让我们的国际化资源生效,就需要让我们自己的Locale生效! 我们去自己写一个自己的LocaleResolver,可以在链接上携带区域信息!

在这里插入图片描述

修改一下前端页面的跳转连接

中文English

我们去写一个处理的组件类!:MyLocaleResolver

package com.loey.config;import com.mysql.cj.util.StringUtils;import org.springframework.web.servlet.LocaleResolver;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Locale;//可以在链接上携带区域信息public class MyLocaleResolver implements LocaleResolver {
//解析请求 @Override public Locale resolveLocale(HttpServletRequest request) {
String language = request.getParameter("l"); Locale locale = Locale.getDefault();//如果没有获取到就使用系统默认的 //如果请求链接不为空 if(!StringUtils.isNullOrEmpty(language)){
//分割请求参数 String[] split = language.split("_"); //国家、地区 locale = new Locale(split[0], split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}}

为了让我们的区域化信息能够生效,我们需要再配置一下这个组件!在我们自己的MvcConofig下添加 bean;

@Beanpublic LocaleResolver localeResolver() {
return new MyLocaleResolver();}

我们重启项目,来访问一下,发现点击按钮可以实现成功切换!搞定收工!

在这里插入图片描述

index.html

			
Signin Template for Bootstrap

转载地址:http://xmuki.baihongyu.com/

你可能感兴趣的文章
Selenium-webdriver系列教程(9)——如何操作select下拉框
查看>>
Selenium-webdriver系列教程(10)——如何智能的等待页面加载完成
查看>>
Robotium测试NotePad(一)
查看>>
Robotium测试NotePad(二) //测试添加文本
查看>>
ksh 多进程
查看>>
ksh 命令分隔符
查看>>
Linux 精萃
查看>>
sed 精萃
查看>>
awk 精萃
查看>>
awk 注释
查看>>
GROUPING SETS、ROLLUP、CUBE
查看>>
数据类型和变量
查看>>
表连接(JOIN)
查看>>
游标(Cursor)
查看>>
复合语句(compound statement)
查看>>
DB2 物化查询表
查看>>
IF 语句
查看>>
循环语句
查看>>
DB2 临时表
查看>>
ITERATE、LEAVE、GOTO和RETURN
查看>>