栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

[模板引擎] thymeleaf 独立环境使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[模板引擎] thymeleaf 独立环境使用

目录

资料引入依赖定义模板使用附

1. 工程结构示例2. 执行结果示例3. 示例源码

资料

官网

引入依赖

        
            org.thymeleaf
            thymeleaf
            3.1.0.M1
        
    
定义模板

以html为例




    
    Title


    


使用

以下测试代码分别演示了输出到变量和流的情况,更多用法可以参考官网或源代码

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class ThymeleafDemo {
    public static void main(String[] args) throws IOException {

        // 定义、设置模板解析器
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        // 设置模板类型 # https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#what-is-thymeleaf
        // HTML、XML、TEXT、JAVAscript、CSS、RAW
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setPrefix("/template/");
        templateResolver.setSuffix(".html");
        // 定义模板引擎
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        // 定义数据模型
        Context context = new Context();
        context.setVariable("username","张三");
        // 渲染模板 (输出到变量(控制台))
        System.out.println(templateEngine.process("test", context));
        // 输出到流(文件)
        File file = new File("E:\","test_out.html");
        Writer write = new FileWriter(file);
        templateEngine.process("test",context,write);
    }
}

附 1. 工程结构示例

2. 执行结果示例


3. 示例源码

https://gitee.com/master336/template-engine-demo

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721181.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号