2009-09-16 13 views
47

Bir String’ten Velocity Template oluşturmanın en iyi yolu nedir?Dize Hız Şablonu nasıl kullanılır?

I dize veya StringReader geçebilir Velocity.evaluate yöntem farkında fakat bunu yapmak için iyi bir yol (Şablon örneği oluşturma gibi herhangi bir avantaj) vardır meraklı değilim.

cevap

64

Bazı ek yük ayrıştırma şablonu var. Şablonunuz büyükse, şablonun önceden ayrılmasıyla bazı performans kazancı görebilir ve tekrar tekrar kullanabilirsiniz. Sen böyle bir şey yapabilirsiniz

RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); 
StringReader reader = new StringReader(bufferForYourTemplate); 
Template template = new Template(); 
template.setRuntimeServices(runtimeServices); 

/* 
* The following line works for Velocity version up to 1.7 
* For version 2, replace "Template name" with the variable, template 
*/ 
template.setData(runtimeServices.parse(reader, "Template name"))); 

template.initDocument(); 

Sonra her ayrıştırma olmadan defalarca template.merge() çağırabilir.

BTW, String'i doğrudan Velocity.evaluate()'a geçirebilirsiniz.

+0

. Teşekkürler. Diğer kişilerin başvurusu için runtimeServices, org.apache.velocity.runtime.RuntimeInstance – tomsame

+0

Eksik bir satırın bir örneğidir. Tamlığı için onu ekledim. Velocity.Evaluate'den bahsetmek için –

+12

+1, tam da aradığım şey bu. –

10

Yukarıdaki örnek kod benim için çalışıyor. Velocity sürüm 1.7 ve log4j kullanır.

private static void velocityWithStringTemplateExample() { 
    // Initialize the engine. 
    VelocityEngine engine = new VelocityEngine(); 
    engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute"); 
    engine.setProperty("runtime.log.logsystem.log4j.logger", LOGGER.getName()); 
    engine.setProperty(Velocity.RESOURCE_LOADER, "string"); 
    engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName()); 
    engine.addProperty("string.resource.loader.repository.static", "false"); 
    // engine.addProperty("string.resource.loader.modificationCheckInterval", "1"); 
    engine.init(); 

    // Initialize my template repository. You can replace the "Hello $w" with your String. 
    StringResourceRepository repo = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT); 
    repo.putStringResource("woogie2", "Hello $w"); 

    // Set parameters for my template. 
    VelocityContext context = new VelocityContext(); 
    context.put("w", "world!"); 

    // Get and merge the template with my parameters. 
    Template template = engine.getTemplate("woogie2"); 
    StringWriter writer = new StringWriter(); 
    template.merge(context, writer); 

    // Show the result. 
    System.out.println(writer.toString()); 
} 

A similar yüzden bir soru.

+0

Vay, bu benzersiz bir yaklaşım. hız motor yük kaynağı nasıl görünüyor iç uygulama, özellik "string.resource.loader.class" değişebilir mi? –

0

Velocity 2 şablon olarak dize dönüştürmek için başka bir seçenek yapmak JSR223 Java Scripting Language Framework entegre edilebilir: Ben arıyordu Tam olarak ne

ScriptEngineManager manager = new ScriptEngineManager(); 
manager.registerEngineName("velocity", new VelocityScriptEngineFactory()); 
ScriptEngine engine = manager.getEngineByName("velocity"); 


System.setProperty(VelocityScriptEngine.VELOCITY_PROPERTIES, "path/to/velocity.properties"); 
String script = "Hello $world"; 
Writer writer = new StringWriter(); 
engine.getContext().setWriter(writer); 
Object result = engine.eval(script); 
System.out.println(writer);