thymeleaf的手動渲染HTML模板


thymeleaf的手動渲染HTML模板

現在很多公司都在thymeleaf作為前端的顯示,但是剛看了一份博客,現在還有人在不斷的詬病thymeleaf的性能問題,然后聽說了一個超級牛逼的叫beetl.其實就是下面這個博客

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

,然后看了看這個Beetl的東西,感覺確實很牛逼啊,但是不在今天的博客范圍內,以后有機會可以試試,為什么我不寫freemaker,因為我覺得語法太惡心,想當年,唉,真是往事不堪回首啊,我現在還覺得freemaker的語法惡心.....

言歸正傳:下面我們來手動渲染一段html代碼和一個html頁面

 

添加依賴:

  1.  
    <!-- Thymeleaf 模板引擎 -->
  2.  
    <dependency>
  3.  
    <groupId>org.thymeleaf</groupId>
  4.  
    <artifactId>thymeleaf</artifactId>
  5.  
    <version>3.0.9.RELEASE</version>
  6.  
    </dependency>

1.封裝一個渲染的工具類:

  1.  
    import org.thymeleaf.TemplateEngine;
  2.  
    import org.thymeleaf.context.Context;
  3.  
    import java.util.Map;
  4.  
     
  5.  
    /**
  6.  
    * @author zk
  7.  
    * @Description:
  8.  
    * @date 2018-11-14 10:34
  9.  
    */
  10.  
    public class HTMLTemplateUtils {
  11.  
     
  12.  
    private final static TemplateEngine engine=new TemplateEngine();
  13.  
     
  14.  
    /**
  15.  
    * 使用 Thymeleaf 渲染 HTML
  16.  
    * @param template HTML模板
  17.  
    * @param params 參數
  18.  
    * @return 渲染后的HTML
  19.  
    */
  20.  
    public static String render(String template,Map <String,Object> params){
  21.  
    Context context = new Context();
  22.  
    context.setVariables(params);
  23.  
    return engine.process(template,context);
  24.  
    }
  25.  
     
  26.  
    }

2.測試:

  1.  
    public class Test {
  2.  
    public static void main(String[] args) {
  3.  
     
  4.  
    String template = " <p th:text='${title}'></p>";
  5.  
    HashMap <String, Object> map = new HashMap<>();
  6.  
    map.put("title","hello world");
  7.  
    String render = HTMLTemplateUtils.render(template, map);
  8.  
    System.out.println("渲染之后的字符串是:"+render);
  9.  
     
  10.  
    }
  11.  
    }

這里運行后會輸出:渲染之后的字符串是:<p>hello world</p>

達到了我們想要的渲染的效果,其實就是一個字符串的替換.....

 

下面我們渲染一個html文件.准備一個 example.html  放在resources下面

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Title</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    <h1 th:text="${name}">列表名稱</h1>
  9.  
    <ul>
  10.  
    <li th:each="item: ${array}" th:text="${item}">條目</li>
  11.  
    </ul>
  12.  
    </body>
  13.  
    </html>

寫一個測試的類:

  1.  
    public class HTMLTest2 {
  2.  
     
  3.  
    public static void main(String[] args) throws IOException {
  4.  
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
  5.  
    //模板所在目錄,相對於當前classloader的classpath。
  6.  
    resolver.setPrefix("");
  7.  
    //模板文件后綴
  8.  
    resolver.setSuffix(".html");
  9.  
    TemplateEngine engine = new TemplateEngine();
  10.  
    engine.setTemplateResolver(resolver);
  11.  
     
  12.  
    //構造上下文(Model)
  13.  
    Context context = new Context();
  14.  
    context.setVariable("name", "三國人物");
  15.  
    context.setVariable("array", new String[]{"曹操", "劉備", "孫權", "漢獻帝"});
  16.  
     
  17.  
    //渲染模板
  18.  
    FileWriter writer = new FileWriter("result.html");
  19.  
    engine.process("example",context,writer);
  20.  
     
  21.  
    //這個example.html 放在resources 下面.這樣機會生成一個result.html文件,結果都已經放進去了.
  22.  
     
  23.  
     
  24.  
    }
  25.  
    }

 

我們這里把渲染后的結果到result.html 中

運行程序就會生成一個result.html 內容是:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Title</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    <h1>三國人物</h1>
  9.  
    <ul>
  10.  
    <li>曹操</li>
  11.  
    <li>劉備</li>
  12.  
    <li>孫權</li>
  13.  
    <li>漢獻帝</li>
  14.  
    </ul>
  15.  
    </body>
  16.  
    </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)
 */

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM