Spring Boot 整合 Thymeleaf 模板引擎(三) 之 实现国际化 2022-03-17 04:40 94阅读 0赞 # **1.application.yml配置** # #thymeleaf模板配置 spring: thymeleaf: prefix: classpath:/templates/ suffix: .html cache: false #热部署文件,false页面不产生缓存,及时更新,true开启缓存 messages: basename: messages/demo #指定外部属性文件位置 # **2.properties文件** # # ![20190222152016901.png][] # demo\_en\_US.properties global.welcome=welcome~!xiaoming demo\_zh\_CN.properties global.welcome=欢迎,小明 # 3.TemplateResolver实现语言区域解析器 # import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Component; import org.springframework.web.servlet.LocaleResolver; import org.thymeleaf.util.StringUtils; @Component public class TemplateResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest request) { Locale locale = Locale.getDefault();//默认 String l = request.getParameter("language"); HttpSession session = request.getSession(); if(!StringUtils.isEmpty(l)){ String[] split = l.split("_"); locale = new Locale(split[0],split[1]); session.setAttribute("language", locale); }else { Object lang = session.getAttribute("language"); if(lang==null) { session.setAttribute("language", locale); }else { locale = (Locale) lang; } } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { // TODO Auto-generated method stub } } # 4.语言区域解析器配置springmvc容器内 # package priv.gitonlie.thymeleaf.configure; import org.springframework.beans.factory.annotation.Autowired; 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.WebMvcConfigurationSupport; @Configuration public class TemplateConfigurerAdapter extends WebMvcConfigurationSupport { @Autowired private TemplateResolver resolver; @Bean public LocaleResolver localeResolver(){ return resolver; } } # 5.新建Controller # @Controller public class DemoController { @RequestMapping("index") public String index() { return "index"; } } # 6.新建index.html # <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf Demo演示页面</title> </head> <body> <a href="" th:href="@{/index(language='zh_CN')}">中文</a><br/> <a href="" th:href="@{/index(language='en_US')}">English</a><br/> <p th:text="#{global.welcome}">welcome to my demo page</p> </body> </html> 中文版 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA5NTc2NDU_size_16_color_FFFFFF_t_70][] 英文版 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA5NTc2NDU_size_16_color_FFFFFF_t_70 1][] [20190222152016901.png]: /images/20220317/6da7638d8f164659a5601b005cca0590.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA5NTc2NDU_size_16_color_FFFFFF_t_70]: /images/20220317/91d4777b6708427cb354fd8405545627.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA5NTc2NDU_size_16_color_FFFFFF_t_70 1]: /images/20220317/8f6a48b7c77247a0beab8b384d487887.png
相关 Spring Boot模板引擎(Thymeleaf) 背景 在前后端分离架构大行其道的当下,为什么Thymeleaf还未被淘汰,它到底有什么不可替代的优势? 1、易被对搜索引擎友好,它由服务端渲染,将页面直接输出到浏览器中 短命女/ 2022年09月09日 06:14/ 0 赞/ 94 阅读
相关 Spring Boot集成Thymeleaf模板引擎 一、Thymeleaf 模板介绍 Spring Boot 推荐使用Thymeleaf 来代替传统开发中的JSP,那么什么是Thymeleaf 模板引擎呢?下面就来简单的介 不念不忘少年蓝@/ 2022年05月27日 23:19/ 0 赞/ 133 阅读
相关 Spring Boot 整合 Thymeleaf 模板引擎(一) 之 搭建 Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。 Spring £神魔★判官ぃ/ 2022年04月10日 14:22/ 0 赞/ 123 阅读
相关 spring boot整合thymeleaf模板 spring boot整合thymeleaf模板 首先需要引入依赖 博主用的是gradle管理jar包, ![在这里插入图片描述][201901072205063 落日映苍穹つ/ 2022年03月29日 04:15/ 0 赞/ 121 阅读
相关 Spring Boot 整合 Thymeleaf 模板引擎(三) 之 实现国际化 1.application.yml配置 thymeleaf模板配置 spring: thymeleaf: prefix: 小鱼儿/ 2022年03月17日 04:40/ 0 赞/ 95 阅读
相关 Spring Boot 整合 Thymeleaf 模板引擎(二) 之 外部属性文件 1.application.yml配置 thymeleaf模板配置 spring: thymeleaf: prefix 深碍√TFBOYSˉ_/ 2022年03月17日 04:20/ 0 赞/ 246 阅读
相关 Spring Boot 整合 Thymeleaf 模板引擎(四) 之 表达式语法 注:这里只列举常用的表达式语法,更多其他详细参详thymeleaf官方文档[https://www.thymeleaf.org/doc/tutorials/3.0/thymel 本是古典 何须时尚/ 2022年03月16日 08:20/ 0 赞/ 142 阅读
相关 Spring Boot 整合thymeleaf 模板引擎 Thymeleaf模板引擎是一个和Velocity、FreeMarker类似的模板引擎,它支持xml/xhtml/html5,且提供额外的模块与Spring MVC集成, 布满荆棘的人生/ 2022年01月06日 10:37/ 0 赞/ 174 阅读
相关 Spring Boot Thymeleaf 模板引擎的使用 Spring Boot 中可以支持很多模板引擎,`Thymeleaf` 是 Spring Boot 官方推荐使用的模板引擎,虽然在社区 `Thymeleaf` 的性能被许多人所 £神魔★判官ぃ/ 2021年11月01日 09:56/ 0 赞/ 274 阅读
相关 Spring Boot(三)整合Thymeleaf Spring Boot HTML html只能通过异步的方式,HTML是可以接收的;同步的方式不能接收,在后台返回一个页面一个java对象是读取不出来的 spring ╰+攻爆jí腚メ/ 2021年08月30日 21:34/ 0 赞/ 256 阅读
还没有评论,来说两句吧...