java 模板引擎 jetbrick-template

1 概述 Overview

jetbrick-template 是一个新一代 Java 模板引擎,具有高性能和高扩展性。 适合于动态 HTML 页面输出或者代码生成,可替代 JSP 页面或者 Velocity 等模板。 指令和 Velocity 相似,表达式和 Java 保持一致,易学易用。

  • 支持类似于 Velocity 的多种指令
  • 支持静态编译
  • 支持编译缓存
  • 支持热加载
  • 支持类型推导
  • 支持泛型
  • 支持可变参数方法调用
  • 支持方法重载
  • 支持类似于 Groovy 的方法扩展
  • 支持函数扩展
  • 支持自定义标签 #tag
  • 支持宏定义 #macro
  • 支持布局 Layout

2 简单易用的指令

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 的比较

3 卓越性能 Performance

jetbrick-template 将模板编译成 Java ByteCode 运行,并采用强类型推导,无需反射和减少类型转换。渲染速度等价于 Java 硬编码。比 Velocity 等模板快一个数量级。 比 JSP 也快,因为 JSP 只有 Scriptlet 是编译的,Tag 和 EL 都是解释执行的。 而 jetbrick-template 是全编译的。

performance

在 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)

4 易于集成 Integrate

可以和市面上常见的 Web MVC framework 进行集成。

具体集成方法,请参考: Web 框架集成

也可以和 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值