4_3.springboot2.x之默认访问首页和国际化
1、默认访问首页
1.引入thymeleaf和引入bootstrap
<!--引入thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--引入bootstrap-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.0</version>
</dependency>
2.新建mvc配置类
//@EnableWebMvc
@Configuration//扩展springmvc功能
public class MyMvcConfig implements WebMvcConfigurer {
//用来做视图映射 浏览器发送 /和/index.htm 请求来到 login页面(由thymeleaf提供)
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
}
application.properties
server.servlet.context-path=/crud
配置web应用的请求路径
2、国际化
springmvc国际化的配置:
1)、编写国际化配置文件;
2)、使用ResourceBundleMessageSource管理国际化资源文件
3)、在页面使用fmt:message取出国际化内容
springboot自动化的配置:
步骤:
1)、编写国际化配置文件,抽取页面需要显示的国际化消息
login.properties
login_en_US.properties
log.btn=Sign In
login.password=Password
login.Remember=Remember me
login.tip=Please sign in
login.username=Username
中文环境类似
2)、SpringBoot自动配置好了管理国际化资源文件的组件;
@Configuration
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration {
//添加基础名配置类,配置文件可以直接放在类路径下叫messages.properties;
@Bean
@ConfigurationProperties(prefix = "spring.messages")
public MessageSourceProperties messageSourceProperties() {
return new MessageSourceProperties();
}
@Bean
public 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;
}
}
application.properties
spring.messages.basename=i18n.login, i18n.language
3)、去页面获取国际化的值
由于使用thymeleaf,用#{}取国际化信息
<input type="text" name="username" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
若这里有中文乱码,请参考idea中properties中文乱码怎么解决。
效果:根据浏览器语言设置的信息切换了国际化;
4)、点击链接切换国际化
原理解析:
springmvc中:国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);
WebMvcAutoConfiguration
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {
if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
//根据请求头request中获取区域信息,
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
自定义区域信息解析器:
html页面:
<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>
区域信息解析器:
package com.spboot.springboot04.component;
import org.springframework.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 l = request.getParameter("l");
Locale locale = Locale.getDefault();//根据浏览器默认
if(!StringUtils.isEmpty(l)){
String[] s = l.split("_");
locale = new Locale(s[0],s[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
将自己添加的区域信息解析器添加到springmvc配置类中,从而添加到容器中:
@Configuration//扩展springmvc功能
public class MyMvcConfig implements WebMvcConfigurer {
.
.
.
.
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
测试:
4_3.springboot2.x之默认访问首页和国际化的更多相关文章
- 怎么修改tomcat默认访问首页
一般情况下安装好tomcat之后我们的默认访问首页是index了,但我们如果要修改或增加一个默认首页,我们可参考下面办法来解决. 通过 ip:port 访问到的是 tomcat 的管理页面,其他常规部 ...
- 设置java web工程中默认访问首页的几种方式
1.demo中最常见的方式是在工程下的web.xml中设置(有时候根据业务可能需要设置action,在action中处理逻辑加载跳转什么的,比较少): <welcome-file-list> ...
- tomcat修改默认访问首页
找到conf下server.xml文件修改如下位置内容 <Host name="localhost" appBase="webapps" unpackWA ...
- SpringBoot:扩展SpringMVC、定制首页、国际化
目录 扩展使用SpringMVC 如何扩展SpringMVC 为何这么做会生效(原理) 全面接管SpringMVC 首页实现 页面国际化 SpringBoot扩展使用SpringMVC.使用模板引擎定 ...
- 8、SpringBoot-CRUD默认访问的首页以及thyleaf的静态文件引入/WebMvcConfigurer / WebMvcConfigurationSupport
1.导入资源 2.默认的访问首页 (1).将代码写在controller中 @RequestMapping({"/","index.html"}) public ...
- 更改CI框架默认访问路径及去掉index.php
下面是去掉index.php的操作 PHP CodeIgniter(CI)去掉 index.php - Langjun - 博客园 设置访问的默认路径是在
- Phpcms安装前后域名默认访问路径
一.安装前 phpcms下载后index.html文件内容如下图,在本地服务器配置项目虚拟域名后,访问域名后直接跳到:域名/install/install.php,然后出现安装界面. 二.安装之后 提 ...
- 0012SpringBoot访问首页
有三种方式可以实现访问首页: 第一种: 定义一个Controller,定义请求方法和返回内容,方法内容如下: @RequestMapping({"/","/index&q ...
- Apache 创建虚拟主机目录和设置默认访问页面
虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...
随机推荐
- SOLID设计原则
SOLID设计原则 Single Responsibility Principle单一职责原则 单一职责原则(SRP)表明一个类有且只有一个职责. 一个类就像容器一样,它能添加任意数量的属性.方法等. ...
- canvas画布导出图片并下载
近期在学习关于画布知识,关于 画布导出图片, 在导出jpeg格式的图片时,会发现图片背景色变成了黑色,原因是画布透明的地方 默认转成了黑色,可以在绘制画布前设置透明处背景色为白色. // 背景色转换成 ...
- php mysql函数库总结(一)
连接mysqlresource mysql_connect(host,username,password);设置交互字符集bool mysql_set_charset("utf8|gbk&q ...
- PHP ftp_raw() 函数
定义和用法 ftp_raw() 函数向 FTP 服务器发送一个 raw 命令. 语法 ftp_raw(ftp_connection,command) 参数 描述 ftp_connection 必需.规 ...
- [转]gulp打包工具总结
与grunt类似,gulp也是构建工具,但相比于grunt的频繁IO操作,gulp的流操作能更快更便捷地完成构建工作.gulp借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级 ...
- 秒懂神经网络---BP神经网络具体应用不能说的秘密.
秒懂神经网络---BP神经网络具体应用不能说的秘密 一.总结 一句话总结: 还是要上课和自己找书找博客学习相结合,这样学习效果才好,不能单视频,也不能单书 BP神经网络就是反向传播神经网络 1.BP神 ...
- Go中的switch fallthrough
Go基础 switch sExpr { case expr1: some instructions case expr2: some other instructions case expr3: ...
- jvm-多线程
多线程的目的 为什么要使用多线程?可以简单的分两个方面来说: 在多个cpu核心下,多线程的好处是显而易见的,不然多个cpu核心只跑一个线程其他的核心就都浪费了: 即便不考虑多核心,在单核下,多线程也是 ...
- 中断控制及basepri 与 basepri_max
1.总开关 每个CPU有一个中断总开关.通过CPU中断控制寄存器实现.Cortex-M的中断控制寄存器包括:FAULTMASK.PRIMASK.BASEPRI.BASEPRI_MAX.总开关的本质是变 ...
- centos 7 设置IP地址
先说下安装方式:我是采用的最小化安装 虚拟机软件:vmware 设置IP有两种情况,动态IP和静态IP,下面分别说明两种IP地址的设置方法 1.动态IP 条件:路由设置了动态分配IP地址(一般默认是动 ...