thymeleaf的手动渲染HTML模板

版权声明:本文为博主原创文章,欢迎大家讨论,未经博主允许不得转载. https://blog.csdn.net/u010398771/article/details/84062903

现在很多公司都在thymeleaf作为前端的显示,但是刚看了一份博客,现在还有人在不断的诟病thymeleaf的性能问题,然后听说了一个超级牛逼的叫beetl.其实就是下面这个博客

https://my.oschina.net/xiandafu/blog/1505526?p=4

,然后看了看这个Beetl的东西,感觉确实很牛逼啊,但是不在今天的博客范围内,以后有机会可以试试,为什么我不写freemaker,因为我觉得语法太恶心,想当年,唉,真是往事不堪回首啊,我现在还觉得freemaker的语法恶心.....

言归正传:下面我们来手动渲染一段html代码和一个html页面

添加依赖:

<!-- Thymeleaf 模板引擎 -->
<dependency>
   <groupId>org.thymeleaf</groupId>
   <artifactId>thymeleaf</artifactId>
   <version>3.0.9.RELEASE</version>
</dependency>

1.封装一个渲染的工具类:

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.util.Map;

/**
 * @author zk
 * @Description:
 * @date 2018-11-14 10:34
 */
public class HTMLTemplateUtils {

    private final static TemplateEngine engine=new TemplateEngine();

    /**
     * 使用 Thymeleaf 渲染 HTML
     * @param template  HTML模板
     * @param params 参数
     * @return  渲染后的HTML
     */
    public static String render(String template,Map<String,Object> params){
        Context context = new Context();
        context.setVariables(params);
        return engine.process(template,context);
    }

}

2.测试:

public class Test {
    public static void main(String[] args) {

        String template = "<p th:text='${title}'></p>";
        HashMap<String, Object> map = new HashMap<>();
        map.put("title","hello world");
        String render = HTMLTemplateUtils.render(template, map);
        System.out.println("渲染之后的字符串是:"+render);

    }
}

这里运行后会输出:渲染之后的字符串是:<p>hello world</p>

达到了我们想要的渲染的效果,其实就是一个字符串的替换.....

下面我们渲染一个html文件.准备一个 example.html  放在resources下面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:text="${name}">列表名称</h1>
<ul>
    <li th:each="item: ${array}" th:text="${item}">条目</li>
</ul>
</body>
</html>

写一个测试的类:

public class HTMLTest2 {

    public static void main(String[] args) throws IOException {
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        //模板所在目录,相对于当前classloader的classpath。
        resolver.setPrefix("");
        //模板文件后缀
        resolver.setSuffix(".html");
        TemplateEngine engine = new TemplateEngine();
        engine.setTemplateResolver(resolver);

        //构造上下文(Model)
        Context context = new Context();
        context.setVariable("name", "三国人物");
        context.setVariable("array", new String[]{"曹操", "刘备", "孙权", "汉献帝"});

        //渲染模板
        FileWriter writer = new FileWriter("result.html");
        engine.process("example",context,writer);

        //这个example.html 放在resources 下面.这样机会生成一个result.html文件,结果都已经放进去了.


    }
}

我们这里把渲染后的结果到result.html 中

运行程序就会生成一个result.html 内容是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>三国人物</h1>
<ul>
    <li>曹操</li>
    <li>刘备</li>
    <li>孙权</li>
    <li>汉献帝</li>
</ul>
</body>
</html>

也可以手动的渲染web请求来的,下次咱们再补充上来.敬请期待

其实渲染最后都是调用的这个方法:

/**
 * org.thymeleaf.templateparser.ITemplateParser#parseStandalone(org.thymeleaf.IEngineConfiguration, java.lang.String, java.lang.String, java.util.Set, org.thymeleaf.templateresource.ITemplateResource, org.thymeleaf.templatemode.TemplateMode, boolean, org.thymeleaf.engine.ITemplateHandler)
 */

达到了取值的效果.其实就是一个放入值,取出,替换原来变量的过程

2018年11月14日11:16:42

猜你喜欢

转载自blog.csdn.net/u010398771/article/details/84062903
今日推荐