jetbrick-template 是一個新一代 Java 模板引擎,具備高性能和高擴展性。 適合於動態 HTML 頁面輸出或者代碼生成,可替代 JSP 頁面或者 Velocity 等模板。 指令和 Velocity 類似,表達式和 Java 保持一致,易學易用。html
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
jetbrick-template 將模板編譯成 Java ByteCode 運行,並採用強類型推導,無需反射和減小類型轉換。渲染速度等價於 Java 硬編碼。比 Velocity 等模板快一個數量級。 比 JSP 也快,由於 JSP 只有 Scriptlet 是編譯的,Tag 和 EL 都是解釋執行的。 而 jetbrick-template 是全編譯的。github
在 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)瀏覽器
能夠和市面上常見的 Web MVC framework 進行集成。緩存
具體集成方法,請參考: Web 框架集成app
也能夠和 Spring Ioc 進行集成,請參考:如何在 Spring 中使用 JetEngine框架
具備詳細的模板解析和編譯錯誤提示,出錯提示能夠定位到原始模板所在的行號。函數
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)
...
|