一款好用的国内模型引擎Beetl

本文介绍了一款名为Beetl的高性能Java模板引擎,并通过示例代码展示了如何使用它来快速生成模板。Beetl相比Freemarker性能提升了六倍,适用于需要高效模板渲染的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍一款国内同学编写的一款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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值