- 与springboot整合maven配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
- thymeleaf的简单配置及使用
(1) 添加maven依赖
(2) 在resources/templates目录下添加模板
(3) 编写Controller - 配置文件配置信息
#启用模板缓存。 spring.thymeleaf.cache = true #在呈现模板之前检查模板是否存在。 spring.thymeleaf.check-template = true #检查模板位置是否存在。 spring.thymeleaf.check-template-location = true #Content-Type值。 spring.thymeleaf.content-type = text/html #启用MVC Thymeleaf视图分辨率。 spring.thymeleaf.enabled = true #模板编码。 spring.thymeleaf.encoding = utf-8 #应用于模板的模板模式,默认HTML5。另请参见StandardTemplateModeHandlers。 spring.thymeleaf.mode = HTML #在构建URL时预先查看名称的前缀,模板路径。 spring.thymeleaf.prefix = classpath:/templates/ #构建URL时附加到查看名称的后缀,模板后缀名。 spring.thymeleaf.suffix = .html #链中模板解析器的顺序。 #spring.thymeleaf.template-resolver-order = #可以解析的视图名称的逗号分隔列表。 #spring.thymeleaf.view-names = #应该从解决方案中排除的视图名称的逗号分隔列表。 #spring.thymeleaf.excluded-view-names =
- 国际化配置
(1) 新建不同语言的配置文件
home.properties 默认配置文件
home_en_US.properties 英文配置文件
home_zh_CN.properties 中文配置文件
(2) 新家配置,指定语言配置文件目录
properties # i18n为resources下目录,home为语言配置文件前缀 spring.messages.basename=i18n/home
(3) 新增区域解析器package com.zp.demo; import org.springframework.web.servlet.LocaleResolver; import org.thymeleaf.util.StringUtils; 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) { //获取连接路径上的l后的值 l=zh_CN or en_US String l = request.getParameter("l"); //如果没有值则使用默认语言版本 Locale locale = Locale.getDefault(); //如果有值则进行语言切换 if(!StringUtils.isEmpty(l)){ String[] split = l.split("_"); locale = new Locale(split[0],split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } }
(4) 页面添加语言切换按钮package com.zp.demo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; @Configuration public class MyAppConfig { @Bean public LocaleResolver localeResolver() { return new MyLocaleResolver(); } }
(5) 需要国际化的页面需从配置文件中获取数据/index为页面跳转接口,l与解析器中获取的参数一致 <a th:href="@{/index?(l='zh_CN')}">中文</a> <a th:href="@{/index?(l='en_US')}">English</a>
(6) #{}引入可加参数#{}引入的变量会根据区域设置读取不同的文件 <div th:text="#{home.name}"></div>
(7) 消息key及参数都可使用变量页面引用,可设置不可参数 <div th:text="#{home.name('参数1','参数1')}"></div>2 home.properties配置文件配置,可在信息中通过{index}引入参数,index为参数位置索引 home.name = 参数1{0}和{1}
<div th:utext="#{${homeMsgKey}(${session.user.name})}"></div>
- 设置全局变量
package com.zp.demo; import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; /** * 重写configureViewResolvers方法,设置StaticVariables即可 */ @Component public class WebMvcConfigurerAdapter implements WebMvcConfigurer { @Resource(name = "thymeleafViewResolver") private ThymeleafViewResolver thymeleafViewResolver; @Override public void configureViewResolvers(ViewResolverRegistry registry) { if (thymeleafViewResolver != null) { Map<String, Object> vars = new HashMap<>(1); vars.put("version", "1.0.0"); thymeleafViewResolver.setStaticVariables(vars); } WebMvcConfigurer.super.configureViewResolvers(registry); } }
模板引擎(thymeleafAPI)
最新推荐文章于 2022-08-28 20:21:53 发布