2016-08-13 8 views

答えて

0

これは私が文字列のうち、テンプレートを作った方法です。

 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(); 
0

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); 
    } 
} 
関連する問題