我有一个Java字符串,它基本上是一个速度模板。
String vt = "#foreach ($number in [1..34]) $number += $number #end"
String result = *String_vt_calculated_by_Velocity_Engine*;
System.out.println(result);
如何在Java中评估上述字符串并获得结果?
答案 0 :(得分:1)
您可以将其传递给Velocity.evaluate()
(请参阅API中的org.apache.velocity.app.Velocity
)。
答案 1 :(得分:0)
您必须使用Velocity.Evaluate
选中此Document
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class LoadTemplateFromStringTest {
public static void main(String[] args) {
VelocityContext context = new VelocityContext();
StringWriter sw = new StringWriter();
String vt = "#foreach ($number in [1..34]) $number += $number #end";
Velocity.evaluate( context, sw, "", vt);
System.out.println(sw);
}
}