spring boot 与 thymeleaf (1): 国际化
在thymeleaf 里面有个消息表达式: #{...} , 可以借此来实现国际化.
在我使用这个功能的时候, 碰到了一个问题, 按照 JavaEE开发的颠覆者 Spring Boot实战 上面编码的时候, 出现了以下问题, 相信很多人都碰到过.
??home.welcome_zh_CN??
这里推荐一篇博客, 里面有解决办法. 玩转spring boot——国际化
我也想将我自己代码记录下来.
一. 目录预览

这里我并没有按照资料上所述的, 将properties文件建在 index.html 同级别目录下, 也没有将名称命名为: index.properties
之前是这么弄的, 但是有上述问题.
二. properties文件内容
messages.properties:
welcome=welcome here! and 欢迎欢迎!
这个是在, 没有语言匹配上的时候, 才会显示
messages_en_US.properties:
welcome=welcome here!
messages_zh_CN.properties:
welcome=欢迎欢迎!
三. 控制器
package org.elvin.learn.springboot.controller; import org.elvin.learn.springboot.pojo.Book;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList;
import java.util.List; /**
* author: Elvin
* Date: 2017/12/13 9:29
* Description:
*/
@Controller
@RequestMapping("thy")
public class ThyController { @GetMapping("index")
public String index(Model model) {
model.addAttribute("color", "red");
return "thy/index";
}
}
四. 视图
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/>
<title>Title</title>
<!--@ { } 链接网址表达式, 会自动加入虚拟路径-->
<link th:href="@{/bootstrap-3.3.7/css/bootstrap.css}" rel="stylesheet"/>
<link th:href="@{/bootstrap-3.3.7/css/bootstrap-theme.css}" rel="stylesheet"/>
<style th:inline="text">
/*通过[ [ $ { }]]的方式访问model中的属性*/
.bgcolor {
background-color:[[${color}]]
}
</style>
</head>
<body> <div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">欢迎</h3>
<a style="background-color: #0f0f0f" th:href="@{/thy/index?lang=en_US}">English(US)</a>
<a style="background-color: #0f0f0f" th:href="@{/thy/index?lang=zh_CN}">简体中文</a>
</div>
<div class="panel-body">
<!-- $ { }变量表达式, 可以访问model中的属性 -->
<span class="bgcolor" th:utext="#{welcome}">hahaha</span>
</div>
</div>
</body>
</html>
五. 结果
使用火狐浏览器(可以调显示语言).
第一次打开的时候, 已经是能正常显示的了. 不再报错

此时, 调整火狐默认显示语言

刷新页面:

可能有人注意到, 我这里有两个链接, 用来切换显示的. 按照上面的方式, 是不能这么切换的. 此时需要做一些修改
六. 自定义显示
加入配置文件
package org.elvin.learn.springboot.conf; import java.util.Locale; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver; @Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
// 默认语言
//slr.setDefaultLocale(Locale.US);
return slr;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("lang");
return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
这里的默认语言可设置, 也可不设置. 如果不设置, 就会根据浏览器默认语言去判断显示哪一个信息.
七. 结果
这里可以根据 lang 去自己选择显示那种语言了


spring boot 与 thymeleaf (1): 国际化的更多相关文章
- 一个小demo熟悉Spring Boot 和 thymeleaf 的基本使用
目录 介绍 零.项目素材 一. 创建 Spring Boot 项目 二.定制首页 1.修改 pom.xml 2.引入相应的本地 css.js 文件 3.编辑 login.html 4.处理对 logi ...
- Spring Boot整合 Thymeleaf 模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- spring boot 与 thymeleaf (2): 常用表达式
在asp.net mvc 中, 有一个视图解析器, 可以支持Razor语法. 使用起来, 是非常的方便, 并且, 写在前台页面的后台方法, 是可调试的. 但是在java中, 目前我还没有接触到, 像. ...
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- Spring Boot 2 + Thymeleaf:表单字段绑定、表单提交处理
Spring Boot中Thymeleaf对表单处理的一些用法:(1)使用th:field属性:进行表单字段绑定(2)使用ids对象:一般用于lable配合radio或checkbox使用(3)表单提 ...
- 极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- Spring Boot整合Thymeleaf模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- Spring Boot整合Thymeleaf视图层
目录 Spring Boot整合Thymeleaf Spring Boot整合Thymeleaf 的项目步骤 Thymeleaf 语法详解 Spring Boot整合Thymeleaf Spring ...
随机推荐
- [FAILED]Marking disk "DATA02" as an ASM disk
执行删除asm磁盘时报错! [root@rac1 grid]# /etc/init.d/oracleasm deletedisk DATA01 /dev/sdc1 Marking disk " ...
- R12 AR INVOICE 接口表导入
http://blog.csdn.net/fangz0615/article/details/38677085 Purpose 本文介绍了如何通过AR接口表进行AR事务处理(亦称AR发票)导入. Ap ...
- jQuery WeUI V0.4.2 发布
http://www.oschina.net/news/71590/jquery-weui-v0-4-2 jQuery WeUI V0.4.2 发布了! jQuery WeUI 中使用的是官方WeUI ...
- IDHTTP的基本用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- ora-12154 TNS:无法处理服务名疑难处理
折腾了几天的PLSQL,终于解决这个问题,在此分享给需要的朋友 服务器:WindowsServer2008(64位操作系统).Win7(64位操作系统) Oracle版本:10.2.0 问题:安装完O ...
- Constructor in depth
There are two types of constructor:Instance Constructor and Type Constructor(or so-called Static Con ...
- SignalR2简易数据看板演示
软件环境: 1.vs2015.windows7..net4.5 演示说明: 当点击按钮的时候,柱状图数值加1并实时变化 1.首先打开vs2015创建一个mvc项目,并安装SignalR2,具体操作可参 ...
- SQL2014 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
管理员身份运行 cmd -> 输入 netsh winsock reset
- MVC 5使用ViewBag(对象)显示数据
前面Insus.NET有演示使用ViewData来实现控制器与视图的通讯.如果想了解的话,可以从下面两个链接可以查看:<MVC 5使用ViewData(对象)显示数据>http://www ...
- solr 加载 停用/扩展词典
startup.bat 停止词典的效果