java规范化字符模板_java-如何使用字符串作为速度模板?

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(),而无需每次都对其进行解析。

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

ZZ Coder answered 2020-02-06T05:42:03Z

16 votes

上面的示例代码为我工作。 它使用Velocity 1.7版和log4j。

private static void velocityWithStringTemplateExample() {

// Initialize the engine.

VelocityEngine engine = new VelocityEngine();

engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");

engine.setProperty("runtime.log.logsystem.log4j.logger", LOGGER.getName());

engine.setProperty(Velocity.RESOURCE_LOADER, "string");

engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());

engine.addProperty("string.resource.loader.repository.static", "false");

// engine.addProperty("string.resource.loader.modificationCheckInterval", "1");

engine.init();

// Initialize my template repository. You can replace the "Hello $w" with your String.

StringResourceRepository repo = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);

repo.putStringResource("woogie2", "Hello $w");

// Set parameters for my template.

VelocityContext context = new VelocityContext();

context.put("w", "world!");

// Get and merge the template with my parameters.

Template template = engine.getTemplate("woogie2");

StringWriter writer = new StringWriter();

template.merge(context, writer);

// Show the result.

System.out.println(writer.toString());

}

类似的问题。

George Siggouroglou answered 2020-02-06T05:42:29Z

0 votes

Velocity 2可以集成到JSR223 Java脚本语言框架中,该框架提供了另一个将字符串转换为模板的选项:

ScriptEngineManager manager = new ScriptEngineManager();

manager.registerEngineName("velocity", new VelocityScriptEngineFactory());

ScriptEngine engine = manager.getEngineByName("velocity");

System.setProperty(VelocityScriptEngine.VELOCITY_PROPERTIES, "path/to/velocity.properties");

String script = "Hello $world";

Writer writer = new StringWriter();

engine.getContext().setWriter(writer);

Object result = engine.eval(script);

System.out.println(writer);

user7294900 answered 2020-02-06T05:42:49Z

0 votes

RuntimeServices rs = RuntimeSingleton.getRuntimeServices();

StringReader sr = new StringReader("Username is $username");

SimpleNode sn = rs.parse(sr, "User Information");

Template t = new Template();

t.setRuntimeServices(rs);

t.setData(sn);

t.initDocument();

VelocityContext vc = new VelocityContext();

vc.put("username", "John");

StringWriter sw = new StringWriter();

t.merge(vc, sw);

System.out.println(sw.toString());

otmek answered 2020-02-06T05:43:04Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值