java 模板引擎 jetbrick-template

1 概述 Overview

jetbrick-template 是一個新一代 Java 模板引擎,具備高性能和高擴展性。 適合於動態 HTML 頁面輸出或者代碼生成,可替代 JSP 頁面或者 Velocity 等模板。 指令和 Velocity 類似,表達式和 Java 保持一致,易學易用。html

  • 支持相似於 Velocity 的多種指令
  • 支持靜態編譯
  • 支持編譯緩存
  • 支持熱加載
  • 支持類型推導
  • 支持泛型
  • 支持可變參數方法調用
  • 支持方法重載
  • 支持相似於 Groovy 的方法擴展
  • 支持函數擴展
  • 支持自定義標籤 #tag
  • 支持宏定義 #macro
  • 支持佈局 Layout

2 簡單易用的指令

jetbrick-template 指令集和老牌的模板引擎 Velocity 很是類似,易學易用。java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define(List< UserInfo > userlist)
< table >
   < tr >
     < td >序號</ td >
     < td >姓名</ td >
     < td >郵箱</ td >
   </ tr >
   #for (UserInfo user : userlist)
   < tr >
     < td >${for.index}</ td >
     < td >${user.name}</ td >
     < td >${user.email}</ td >
   </ tr >
   #end
</ table >

詳細指令語法,請參考:語法指南。或者和 Velocity 的比較git

3 卓越性能 Performance

jetbrick-template 將模板編譯成 Java ByteCode 運行,並採用強類型推導,無需反射和減小類型轉換。渲染速度等價於 Java 硬編碼。比 Velocity 等模板快一個數量級。 比 JSP 也快,由於 JSP 只有 Scriptlet 是編譯的,Tag 和 EL 都是解釋執行的。 而 jetbrick-template 是全編譯的。github

performance

在 Stream 模式中(Webapp 採用 OutputStream 將文本輸出到瀏覽器),因爲 Java 硬編碼輸出字符串須要進行一次編碼的轉換。 而 jetbrick-template 卻在第一次運行期間就緩存了編碼轉換結果,使得 jetbrick-template 的性能甚至優於 Java 硬編碼。spring

具體測試用例,請參考:Template Engine Benchmark Test (platform: Window 7 x64, Intel i5, 16GB RAM, JDK 1.6.0_41 x64)瀏覽器

4 易於集成 Integrate

能夠和市面上常見的 Web MVC framework 進行集成。緩存

具體集成方法,請參考: Web 框架集成app

也能夠和 Spring Ioc 進行集成,請參考:如何在 Spring 中使用 JetEngine框架

5 友好的錯誤提示

具備詳細的模板解析和編譯錯誤提示,出錯提示能夠定位到原始模板所在的行號。函數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
22:14:51.271 [main] INFO  (JetTemplate.java:68) - Loading template source file: D:\workspace\github\jetbrick-schema-app\bin\config\report\schema.html.jetx
22:14:51.406 [main] ERROR (JetTemplateErrorListener.java:27) - Template parse failed:
D:\workspace\github\jetbrick-schema-app\bin\config\report\schema.html.jetx:37
message: The method getColumnNam() or isColumnNam() is undefined for the type jetbrick.schema.app.model.TableColumn.
33:         </tr>
34:     #for(TableColumn c: t.columns)
35:         <tr style="background-color:white;">
36:             <td>${c.displayName}</td>
37:             <td>${c.columnNam}</td>
                         ^^^^^^^^^
  
Exception in thread "main" jetbrick.commons.exception.SystemException: java.lang.RuntimeException: The method getColumnNam() or isColumnNam() is undefined for the type jetbrick.schema.app.model.TableColumn.
   at jetbrick.commons.exception.SystemException.unchecked(SystemException.java:23)
   at jetbrick.commons.exception.SystemException.unchecked(SystemException.java:12)
   at jetbrick.schema.app.TemplateEngine.apply(TemplateEngine.java:44)
   at jetbrick.schema.app.Task.writeFile(Task.java:83)
   at jetbrick.schema.app.task.SqlReportTask.execute(SqlReportTask.java:19)
   at jetbrick.schema.app.SchemaGenerateApp.taskgen(SchemaGenerateApp.java:56)
   at jetbrick.schema.app.SchemaGenerateApp.main(SchemaGenerateApp.java:74)
Caused by: java.lang.RuntimeException: The method getColumnNam() or isColumnNam() is undefined for the type jetbrick.schema.app.model.TableColumn.
   at jetbrick.template.parser.JetTemplateCodeVisitor.reportError(JetTemplateCodeVisitor.java:1388)
   at jetbrick.template.parser.JetTemplateCodeVisitor.visitExpr_field_access(JetTemplateCodeVisitor.java:665)
   at jetbrick.template.parser.JetTemplateCodeVisitor.visitExpr_field_access(JetTemplateCodeVisitor.java:1)
...
  • 出錯模板:D:\workspace\github\jetbrick-schema-app\bin\config\report\schema.html.jetx
  • 出錯行號:37
  • 錯誤緣由:The method getColumnNam() or isColumnNam() is undefined for the type jetbrick.schema.app.model.TableColumn.
相關文章
相關標籤/搜索