1.Thymeleaf是什么?

Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。

它的主要目标是提供一种优雅的、高度可维护的创建模版的方式。为了实现这一点,它建立在自然模版的概念上,将其逻辑注入到模版中,不会影响模版被用作设计原型。这改善了设计的沟通,弥合了设计和开发团队之间的差距。

2.SpringBoot与Thymeleaf集成


/**
 * 实现ApplicationContextAware是为了得到ApplicationContext对象
 * 
 * @author Administrator
 *
 */
@Configuration
public class ThymeleafConfig implements ApplicationContextAware {

	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	/**
	 * 配置Thymeleaf模板解析器
	 * 
	 * @return
	 */
	@Bean
	public SpringResourceTemplateResolver templateResolver() {
		SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
		templateResolver.setApplicationContext(this.applicationContext);
		// 模板文件前缀
		templateResolver.setPrefix("classpath:/templates/");
		// 模板文件后缀
		templateResolver.setSuffix(".html");
		// 配置Thymeleaf模板模式
		templateResolver.setTemplateMode(TemplateMode.HTML);
		// 默认情况下,模板缓存为true,提供缓存,当希望模板修改时,自动更新模板,则设置为false
		templateResolver.setCacheable(false);
		return templateResolver;
	}

	/**
	 * 配置Thymeleaf模板引擎
	 * 
	 * @return
	 */
	@Bean
	public SpringTemplateEngine templateEngine() {
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		templateEngine.setTemplateResolver(templateResolver());
		templateEngine.setEnableSpringELCompiler(true);
		// 在Thymeleaf模板中添加模板方言,使其模板文件中可以使用shiro标签
		// 当然在这里,需要在pom.xml文件中加入相关依赖:
		// <dependency>
		// <groupId>com.github.theborakompanioni</groupId>
		// <artifactId>thymeleaf-extras-shiro</artifactId>
		// <version>2.0.0</version>
		// </dependency>
		// 当然在这里,会出现以下异常:ClassNotFoundException:org.thymeleaf.dialect.AbstractProcessorDialect.AbstractProcessorDialect
		// 找不到类AbstractProcessorDialect;原因是由于我们使用的SpringBoot版本是1.5.4.RELEASE,在集成Thymeleaf时,它使用的版本是2.1.5.RELEASE,
		// 而在这个版本中没有此类org.thymeleaf.dialect.AbstractProcessorDialect.AbstractProcessorDialect,所以我们应当更改Thymeleaf版本为3.0.7.RELEASE;
		// 在pom.xml配置文件中的<properties>标签中添加如下:
		// <thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
		// <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
		// 当然我们还可以更改thymeleaf-extras-shiro的版本为1.2.1即可
		IDialect dialect = new ShiroDialect();
		templateEngine.addDialect(dialect);
		return templateEngine;
	}

	/**
	 * 配置Thymeleaf视图解析器
	 * 
	 * @return
	 */
	@Bean
	public ThymeleafViewResolver viewResolver() {
		ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
		viewResolver.setTemplateEngine(templateEngine());
		return viewResolver;
	}

}

3.使用Thymeleaf模板引擎



在创建的HTML文件中的<html>标签中添加一个xmlns:th属性,配置如下:



<html xmlns:th="http://www.thymeleaf.org">

4.标准表达式语法


(1)简单表达式


  • 可变表达式:${...}
  • 选择变量表达式:*{...}
  • 消息表达式:#{...}
  • 链接网址表达式:@{...}
  • 片段表达式:~{...}


(2)文字


  • 文本:‘welcome’,...
  • 数字:0,2,4,...
  • 布尔:true,false
  • 空值:null
  • 文字标记:one

(3)文字操作

  • 字符串连接:+
  • 字符串替代:|The name is ${name}|

(4)算数运算

  • 二元运算符:+,-,*,/,%

减号(一元运算符):-(5)布尔运算:

  • 二元运算符:and,or
  • 布尔否定(一元运算符):!,not

(6)比较和相等运算

  • 比较运算符:>,<,>=,<=(gt,lt,ge,le)
  • 相等运算符:= =,!=(eq,ne)

(7)条件运算符:

  • IF-THEN:(if) ? (then)
  • IF-THEN-ELSE:(if) ? (then) : (else)
  • Default:(value) ?: (defaultvalue)

(8)特殊操作符:

  • 无操作符:_

5.表达式基本对象



  • #ctx:上下文对象
  • #vars:上下文变量
  • #locale:上下文区域设置
  • #request:(仅在Web上下文中)HttpServletRequest对象。
  • #response:(仅在Web上下文中)HttpServletResponse对象。
  • #session:(仅在Web上下文中)HttpSession对象。
  • #servletContext:(仅在Web上下文中)ServletContext对象。