1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、看一下配置 xxxProperties
3.页面与controller
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>测试页面</h1>
<p th:text="${msg}"></p>
<!--转义和不转义-->
<p th:utext="${msg2}"></p>
<!--遍历-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
</body>
</html>
@Controller
public class TestController {
@GetMapping("/test")
public String test(Model model){
model.addAttribute("msg","Hello World");
model.addAttribute("msg2","<h1>hello,Thymeleaf<h1>");
model.addAttribute("users",Arrays.asList("coding","qinjiang"));
return "test";
}
}
Thymeleaf语法
我们还可以编写哪些表达式呢?
Variable Expressions: ${…}
获取一些基本的变量值! OGNL;
- 对象的属性,调用方法
- 使用内置的基本对象
${#ctx.locale}
${param.foo}
${session.foo}
${application.foo}
${#request.getAttribute('foo')}
${#servletContext.contextPath}
- 工具对象
${#messages.msg('msgKey')}
${#uris.escapePath(uri)}
${#conversions.convert(object, 'java.util.TimeZone')}
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}
${#calendars.format(cal)}
${#numbers.formatInteger(num,3)}
${#strings.toString(obj)} ${#arrays.toArray(object)}
Selection Variable Expressions: *{…} 选择表达式,和 ${} 是一样的;
Message Expressions: #{…} 国际化内容获取!
Link URL Expressions: @{…} URL表达式; th:href=“@{/login}”
Fragment Expressions: ~{…} 组件化表达式;
Literals (字面量)
Text literals: ‘one text’ , ‘Another one!’ ,… (字符串)
Text operations: (文本操作)
Arithmetic operations: (数学运算)
Boolean operations: (布尔运算)
Comparisons and equality: (比较运算)
Conditional operators: (条件运算符)
官网查询
官网:https://www.thymeleaf.org/documentation.html
thymeleaf官网
github:https://github.com/thymeleaf/thymeleaf/blob/3.0-master
国际化
准备
i18n : 国际化表示的文件夹
如果是一整个完整的页面(文档页)文章量十分大的时候,没有必要做这些细节化的国际化操作!
配置文件
#国际化 spring.messages.basename=i18n.login
前端页面
以Duubo官网为例:
中文:http://dubbo.apache.org/zh-cn/
英文:http://dubbo.apache.org/en-us/
可以发现是通过不同链接来实现国际化映射的!
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() { // 国际化对象解析LocaleResolver
// 如果用户配置了 就使用用户配置的,如果没有用户配置就使用默认的!
if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
// 接收请求头信息关于国际化的
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
在我们的接收请求解析中发现一个重写的方法:
我们自定义
1、修改前端页面的链接
<!-- localhost:8080/index?l=zh_CN thymeleaf中传递参数不用使用? 使用()-->
<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>
2、处理这个请求! 我们是以参数的请求来进行处理的!我们去解析请求构造国际化对象即可
package com.shying.handler;
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 parameter = request.getParameter("l");
// 如果我们没有配置就使用默认的!
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(parameter)){
String[] split = parameter.split("_");
//分析国家和地区
locale=new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
3、注册到IOC中
// id localeResolver 只能为这个,应为SpringBoot回去扫描并是识别
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
4、重启项目,点击测试即可