我需要使用 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();
因此,您可以创建脚本、加载它并以编程方式处理它。