非web环境下使用Thymeleaf模板

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的测试方法都可以右键方法名运行即可。
在这里插入图片描述
最终得到模板和数据合并后的结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值