我有一個(gè) java 字符串,它基本上是一個(gè)速度模板。String vt = "#foreach ($number in [1..34]) $number += $number #end"String result = *String_vt_calculated_by_Velocity_Engine*;System.out.println(result);如何在 Java 中評(píng)估上述字符串并獲得結(jié)果?
2 回答

GCT1015
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
您必須使用Velocity.Evaluate檢查此文檔
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);
}
}
添加回答
舉報(bào)
0/150
提交
取消