springboot+thymeleaf国际化方法一:LocaleResolver
springboot中大部分有默认配置所以开发起项目来非常迅速,仅对需求项做单独配置覆盖即可
spring采用的默认区域解析器是AcceptHeaderLocaleResolver,根据request header中的accept-language值来解析locale,并且是不可变的。
那么想要实现国际化,就要使用SessionLocaleResolver或者CookieLocaleResolver。正如类的名字所示,是按session或cookie中储存的locale值来解析locale。
我就以SessionLocaleResolver举例:
1.建立一个配置类
package com.example.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver; /**
* Created by wq on 2016/8/15.
*/
@Configuration
public class SpringMVC_config {
@Bean(name="localeResolver")
public LocaleResolver localeResolverBean() {
return new SessionLocaleResolver();
}
// @Bean(name="messageSource")
// public ResourceBundleMessageSource resourceBundleMessageSource(){
// ResourceBundleMessageSource source=new ResourceBundleMessageSource();
// source.setBasename("messages");
// return source;
// }
}
注意 name="localeResolver" 是必须的
优先级如下:
session中对应属性(3中有说明)有值则按session来
如果没有但是SessionLocaleResolver设置了默认的locale则按默认值来
// SessionLocaleResolver localeResolver=new SessionLocaleResolver();
// localeResolver.setDefaultLocale(Locale.ENGLISH);
再然后就还是按request header中的accept-language值来
2.建立对应的messages.properties
messages.properties
messages_en.properties
messages_zh_CN.properties
前面注释的代码则可以修改properties的前缀部分,name="messageSource" 同样是必须的
比如 setBasename("msg"); 对应properties则为
msg.properties
msg_en.properties
msg_zh_CN.properties
格式上sys.test=hello、sys.test=你好,应该无需赘述(可能转码会避免一些问题,我这里直接放的中文)
3.controller中切换locale
package com.example.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale; import static org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME; /**
* Created by Administrator on 2016/6/11.
*/
@Controller
public class DemoController {
@Autowired
LocaleResolver localeResolver; @RequestMapping("test")
public String test(HttpServletRequest request, HttpServletResponse response) {
HttpSession session=request.getSession();
localeResolver.setLocale(request,response,Locale.ENGLISH);
System.out.println(session.getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME));
return "messages";
}
}
这里org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME这个字符串常量则是session中默认属性名
可以看一下SessionLocaleResolver的部分源码
public class SessionLocaleResolver extends AbstractLocaleContextResolver {
public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";
public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".TIME_ZONE";
locale默认属性名为
org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE
setLocale是抽象类AbstractLocaleContextResolver中方法
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
this.setLocaleContext(request, response, locale != null?new SimpleLocaleContext(locale):null);
}
然后看SessionLocaleResolver中setLocaleContext
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
Locale locale = null;
TimeZone timeZone = null;
if(localeContext != null) {
locale = localeContext.getLocale();
if(localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext)localeContext).getTimeZone();
}
}
WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
}
本质上就是一些非空判断取默认,最终给session中的对应属性赋值
4.thymeleaf页面中调用
<!DOCTYPE html>
<html lang="zh_CN"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>msg</title>
</head>
<body>
<h1 th:text="${#locale}"></h1>
<h1 th:text="#{sys.test}"></h1>
</body>
</html>
则显示en hello
为了以防万一我还是在上文标红了= =#
end of story
springboot+thymeleaf国际化方法一:LocaleResolver的更多相关文章
- org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service() cannot be found on com.my.blog.springboot.thymeleaf.util.MethodTest type
前言 本文中提到的解决方案,源码地址在:springboot-thymeleaf,希望可以帮你解决问题. 至于为什么已经写了一篇文章thymeleaf模板引擎调用java类中的方法,又多此一举的单独整 ...
- springboot+thymeleaf+pageHelper带条件分页查询
html层 <div> <a class="num"><b th:text="'共 '+ ${result.resultMap['pages ...
- springboot+thymeleaf简单使用
关于springboot想必很多人都在使用,由于公司项目一直使用的是SpringMVC,所以自己抽空体验了一下springboot的简单使用. 环境搭建 springbooot的环境搭建可以说很灵活, ...
- SpringBoot thymeleaf使用方法,thymeleaf模板迭代
SpringBoot thymeleaf使用方法,thymeleaf模板迭代 SpringBoot thymeleaf 循环List.Map ============================= ...
- SpringBoot thymeleaf模板页面没提示,SpringBoot thymeleaf模板插件安装
SpringBoot thymeleaf模板插件安装 SpringBoot thymeleaf模板Html页面没提示 SpringBoot thymeleaf模板页面没提示 SpringBoot t ...
- SpringBoot thymeleaf模板版本,thymeleaf模板更换版本
SpringBoot thymeleaf模板版本 thymeleaf模板更换版本 修改thymeleaf模板版本 ================================ ©Copyright ...
- Springboot+Thymeleaf框架的button错误
---恢复内容开始--- 在做公司项目时,遇到了一个Springboot+Thymeleaf框架问题: 使用框架写网站时,没有标明type类型的button默认成了‘submit’类型,每次点击按钮都 ...
- SpringBoot+Thymeleaf+iView
SpringBoot+Thymeleaf参考: https://www.cnblogs.com/kibana/p/10236187.html 1.Controller: package cn.mmwe ...
- layui表格数据渲染SpringBoot+Thymeleaf返回的数据时报错(Caused by: org.attoparser.ParseException: Could not parse as expression: ")
layui table渲染数据时报错(Caused by: org.attoparser.ParseException: Could not parse as expression: ") ...
随机推荐
- c++拷贝构造函数引用传参
看一道C++面试题: 给出下述代码,分析编译运行的结果,并提供3个选项: A.编译错误 B.编译成功,运行时程序崩溃 C.编译运行正常,输出10 class A { private: int va ...
- Mybatis系列(三)XML
Mybatis系列(三)XML 1.pom.xml依赖: <?xml version="1.0" encoding="UTF-8"?> <pr ...
- Vue2.x-社交网络程序项目的总结
最近几天一直在学习Vue的课程,通过这个项目进行进一步的学习Vue方面的知识.掌握如何使用Vue搭建前端,如何请求Node.js写好的后端接口. 一.实现前后端连载 首先在后端的文件中 vue i ...
- SpannableString设置文本背景色
参考内容: http://blog.csdn.net/harvic880925/article/details/38984705 http://blog.it985.com/14433.html 1. ...
- RatingBar星级拖动条
RatingBar和SeekBar用法类似,他们都继承AbsSeekBar类; RatingBar的xml属性 android:numStars="5" 表示有5颗星 andro ...
- Net基础篇_学习笔记_第十一天_面向对象(关键字new和this)
new关键字 new:用来创建对象的.Person zsPerson=new Person();new帮助我们做了3件事儿:1).在内存中开辟一块空间2).在开辟的空间中创建对象3).调用对象的构造 ...
- 为什么StringBuilder是线程不安全的?StringBuffer是线程安全的?
面试中经常问到的一个问题:StringBuilder和StringBuffer的区别是什么? 我们非常自信的说出:StringBuilder是线程安全的,StirngBuffer是线程不安全的 面试官 ...
- 2019-2020-1 20199314 <Linux内核原理与分析>第二周作业
1.基础学习内容 1.1 冯诺依曼体系结构 计算机由控制器.运算器.存储器.输入设备.输出设备五部分组成. 1.1.1 冯诺依曼计算机特点 (1)采用存储程序方式,指令和数据不加区别混合存储在同一个存 ...
- 关于WebApi的跨域问题
前端调用我后端接口时出现200,跨域问题 解决方案: 在webconfig中加入以下配置就OK了 <configuration> <system.webServer> < ...
- SVN检出后文件没有图标显示
SVN检出后文件没有图标显示 "Win + R"打开运行框,输入"regedit"打开注册表 在注册表编辑界面按"Ctrl + F"快捷 ...