简体   繁体   English

Velocity模板作为Java中的String

[英]Velocity template as a String in Java

I have a java String which is basically a velocity template. 我有一个java String,它基本上是一个速度模板。

String vt = "#foreach ($number in [1..34])  $number += $number  #end"
String result = *String_vt_calculated_by_Velocity_Engine*;
System.out.println(result);

How can I evaluate the above String in Java and get the result? 如何在Java中评估上面的String并获得结果?

您可以将其传递给Velocity.evaluate() (请参阅API中的 org.apache.velocity.app.Velocity )。

You have to use Velocity.Evaluate check this Document 您必须使用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);
  }
}

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

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