如何使用字符串作为力度模板?

编程入门 行业动态 更新时间:2024-10-22 13:47:31
本文介绍了如何使用字符串作为力度模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从字符串创建Velocity模板的最佳方法是什么?

What is the best way to create Velocity Template from a String?

我知道可以通过String或StringReader传递的 Velocity.evaluate 方法,但是我有个更好的办法(例如,创建Template实例的任何优势)).

I'm aware of Velocity.evaluate method where I can pass String or StringReader, but I'm curios is there a better way to do it (e.g. any advantage of creating an instance of Template).

推荐答案

有一些开销分析模板.如果您的模板很大并且反复使用它,则可以通过预先准备该模板来获得一些性能提升.你可以做这样的事情,

There is some overhead parsing template. You might see some performance gain by pre-parsing the template if your template is large and you use it repeatedly. You can do something like this,

RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(bufferForYourTemplate); Template template = new Template(); template.setRuntimeServices(runtimeServices); /* * The following line works for Velocity version up to 1.7 * For version 2, replace "Template name" with the variable, template */ template.setData(runtimeServices.parse(reader, "Template name"))); template.initDocument();

然后,您可以一遍又一遍地调用 template.merge(),而无需每次都对其进行解析.

Then you can call template.merge() over and over again without parsing it everytime.

顺便说一句,您可以将String直接传递给 Velocity.evaluate().

BTW, you can pass String directly to Velocity.evaluate().

更多推荐

如何使用字符串作为力度模板?

本文发布于:2023-11-26 18:24:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用   力度   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!