0
我需要使用Velocity Template概念生成一個Java文件。你能請指導通過這個請。是否可以通過在Apache Velocity中定義模板來生成Java代碼?
Java代碼應該包含一些方法,進口和變量...
感謝, Easwar
我需要使用Velocity Template概念生成一個Java文件。你能請指導通過這個請。是否可以通過在Apache Velocity中定義模板來生成Java代碼?
Java代碼應該包含一些方法,進口和變量...
感謝, Easwar
隨着速度/ 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();
所以你可以創建一個腳本,加載它並以編程方式處理它。