thymeleaf的手動渲染HTML模板
現在很多公司都在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下面
-
-
<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 內容是:
-
-
<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) */