thymeleaf的手動渲染HTML模板
阿新 • • 發佈:2020-08-03
thymeleaf的手動渲染HTML模板
長河2018-11-14 11:18:106833收藏2 分類專欄:Springboot 版權現在很多公司都在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) */