繁体   English   中英

是否可以通过在Apache Velocity中定义模板来生成Java代码?

[英]Is it possible to generate a Java code by defining a template in Apache Velocity?

我需要使用Velocity Template概念生成Java文件。 请您指导一下。

Java代码应包含一些方法,导入和变量...

谢谢,伊斯瓦尔

对于Velocity / Java部分,您或多或少需要这样做:

// factory and an engine instance
VelocityEngineFactory velocityEngineFactory = new VelocityEngineFactory();
VelocityEngine engine = velocityEngineFactory.createVelocityEngine();
// now you need to give the variables you wanna have access from velocity script
VelocityContext context = new VelocityContext(properties);

ByteArrayOutputStream temp = new ByteArrayOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(temp));
// generate the result, where scriptString is the velocity script
engine.evaluate(context, bufferedWriter, null, scriptString);
bufferedWriter.flush();
textResultant = temp.toString();

因此,您可以创建一个脚本,对其进行加载并以编程方式对其进行处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM