本文介绍一款国内同学编写的一款6倍于Freemarker的超高性能的java模板引擎-Beetl
直接上代码示例:
使用该模板总共三步,获取模板配置----绑定参数-----生成,具体测试代码参考:
public class BeetlTest {
@Test
public void test1() throws IOException {
StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("hello,${name}");
t.binding("name", "beetl");
String str = t.render();
Assert.assertEquals("hello,beetl",str);
}
@Test
public void test2() throws IOException {
String root = System.getProperty("user.dir") + File.separator + "target\\classes\\template";
FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("/temp.txt");
t.binding("name", "gender");
t.binding("type", "int");
String str = t.render();
System.out.println(str);
}
@Test
public void test3() throws IOException {
String root = System.getProperty("user.dir") + File.separator + "target\\classes\\template";
FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("/temp.txt");
t.binding("name", "persons");
t.binding("type", "List<String>");
String str = t.render();
System.out.println(str);
}
}
其中temp.txt为模板配置,内容如下:
<%
//var captialName=strutil.toUpperCase(strutil.subStringTo(name,0,1))+strutil.subString(name,1);
var cn=captialtag.captial(name);
%>
public ${type} get${cn}() {
${name}.values();
}
模板中,可以看到其引用到了captialtag.captial的方法,方法实现需要人为告知Beetl模板引擎,通过配置beetl.properties FNP.captialtag = com.zte.sunquan.demo.beetl.CaptialTag
依赖:
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.7.13</version>
</dependency>
更多内容,参考官网:http://ibeetl.com/guide/