0
私はファイルの内容を含むStringを持っているという要件があります。私はvelocityを使ってその文字列のテンプレートを作成しなければなりません。文字列を入力として受け取り、テンプレートを返すベロシティのメソッドはありますか?velocityには、Stringをarugumentとして渡してテンプレートを返すメソッドがありますか?
私はファイルの内容を含むStringを持っているという要件があります。私はvelocityを使ってその文字列のテンプレートを作成しなければなりません。文字列を入力として受け取り、テンプレートを返すベロシティのメソッドはありますか?velocityには、Stringをarugumentとして渡してテンプレートを返すメソッドがありますか?
これは私が文字列のうち、テンプレートを作った方法です。
RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
StringReader reader = new StringReader(stringValue);
SimpleNode node = runtimeServices.parse(reader, "Template name");
Template template = new Template();
template.setRuntimeServices(runtimeServices);
template.setData(node);
template.initDocument();
Velocity.evaluate()
は、あなたが望むものです。 docsの例から:
public class Example2
{
public static void main(String args[])
{
/* first, we init the runtime engine. Defaults are fine. */
Velocity.init();
/* lets make a Context and put data into it */
VelocityContext context = new VelocityContext();
context.put("name", "Velocity");
context.put("project", "Jakarta");
/* lets render a template */
StringWriter w = new StringWriter();
Velocity.mergeTemplate("testtemplate.vm", context, w);
System.out.println(" template : " + w);
/* lets make our own string to render */
String s = "We are using $project $name to render this.";
w = new StringWriter();
Velocity.evaluate(context, w, "mystring", s);
System.out.println(" string : " + w);
}
}