jetbrick-template 是一个新一代 Java 模板引擎,具有高性能和高扩展性。 适合于动态 HTML 页面输出或者代码生成,可替代 JSP 页面或者 Velocity 等模板。 指令和 Velocity 相似,表达式和 Java 保持一致,易学易用。
jetbrick-template 指令集和老牌的模板引擎 Velocity 非常相似,易学易用。
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 的比较。
jetbrick-template 将模板编译成 Java ByteCode 运行,并采用强类型推导,无需反射和减少类型转换。渲染速度等价于 Java 硬编码。比 Velocity 等模板快一个数量级。 比 JSP 也快,因为 JSP 只有 Scriptlet 是编译的,Tag 和 EL 都是解释执行的。 而 jetbrick-template 是全编译的。
在 Stream 模式中(Webapp 采用 OutputStream 将文本输出到浏览器),由于 Java 硬编码输出字符串需要进行一次编码的转换。 而 jetbrick-template 却在第一次运行期间就缓存了编码转换结果,使得 jetbrick-template 的性能甚至优于 Java 硬编码。
具体测试用例,请参考:Template Engine Benchmark Test (platform: Window 7 x64, Intel i5, 16GB RAM, JDK 1.6.0_41 x64)
可以和市面上常见的 Web MVC framework 进行集成。
具体集成方法,请参考: Web 框架集成
也可以和 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)
...
|