java-如何使用字符串作为速度模板?
从字符串创建力度模板的最佳方法是什么?
我知道可以在其中传递String或StringReader的Velocity.evaluate方法,但是我想知道,有一种更好的方法(例如,创建Template实例的任何优势)。
tomsame asked 2020-02-06T05:41:41Z
4个解决方案
73 votes
有一些开销分析模板。 如果您的模板很大并且反复使用它,则可以通过预先准备该模板来获得一些性能提升。 你可以做这样的事情,
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();
然后,您可以一次又一次调用Velocity.evaluate(),而无需每