1、用eclipse创建一个普通的maven工程
在左侧exploer空白处右键——》new——》Maven Project,也可以在Other中搜索到Maven Project。
扩展:
Java Working Set是个虚拟文件夹,可以在有很多工程时,对工程进行分类。
在Package Explorer窗口把虚拟文件夹调出来
右键已有的项目就可以分配到任意的一个working set里。
2、创建Maven后配置pom.xml
引入Thymeleaf依赖包,Junit和其他相关依赖包。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zzz</groupId>
<artifactId>test01_thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test01_thymeleaf</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.14</version>
</dependency>
</dependencies>
</project>
3、编写主程序
com.zzz.test01_thymeleaf.App.java 代码如下(自己创建测试类也可以):
package com.zzz.test01_thymeleaf;
import org.junit.Test;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
public class App
{
//例子一 不提供数据合并
public static void main( String[] args )
{
//创建模板引擎
TemplateEngine engine = new TemplateEngine();
//准备模板
String input = "<input type='text' th:value='hello'/>";
//准备数据
Context context = new Context();
//调用引擎,处理模板和数据
String out = engine.process(input, context);
System.out.println("result:"+out);
}
//例子二 提供数据合并
@Test
public void test02() {
//创建模板引擎
TemplateEngine engine = new TemplateEngine();
//准备模板
String input = "<input type='text' th:value='${name}'/>";
//准备数据
Context context = new Context();
context.setVariable("name", "小明");
//调用引擎,处理模板和数据
String out = engine.process(input, context);
System.out.println("result:"+out);
}
//例子三 提供模板文件,提供数据合并
@Test
public void test03() {
//创建模板引擎
TemplateEngine engine = new TemplateEngine();
//准备模板 读取磁盘中的所有资源的索引
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
//加载进引擎
engine.setTemplateResolver(resolver);
//准备数据
Context context = new Context();
context.setVariable("name", "小明");
//调用引擎,找到模板,处理模板和数据
String out = engine.process("test01.html", context);
System.out.println("result:"+out);
}
//例子四 提供模板文件,提供数据合并 设置模板文件前缀后缀
@Test
public void test04() {
//创建模板引擎
TemplateEngine engine = new TemplateEngine();
//准备模板 读取磁盘中的所有资源的索引
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("templates/");
resolver.setSuffix(".html");
//加载进引擎
engine.setTemplateResolver(resolver);
//准备数据
Context context = new Context();
context.setVariable("name", "小明");
//调用引擎,找到模板,处理模板和数据
String out = engine.process("test02", context);
System.out.println("result:"+out);
}
}
例子三和四用到了外部模板文件html,我们先创建resources文件夹,并把它设置为资源目录。
注意:要选main下创建
设置resources文件夹为资源目录:
4、外部模板文件
创建如下目录结构:
test01.html代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
test01模板:<input type='text' th:value='${name}'/>
</body>
</html>
test01.html代码只需把test01.html代码中test01修改成test02即可。
5、运行测试
main方法或者Junit @Test的测试方法都可以右键方法名运行即可。
最终得到模板和数据合并后的结果。