2009-09-16 17 views
47

स्ट्रिंग से वेग टेम्पलेट बनाने का सबसे अच्छा तरीका क्या है?स्ट्रिंग को Velocity टेम्पलेट के रूप में कैसे उपयोग करें?

मैं Velocity.evaluate तरीका है जहाँ मैं स्ट्रिंग या StringReader पारित कर सकते हैं के बारे में पता है, लेकिन मैं कलाकृतियां हूँ वहाँ यह करने के लिए एक बेहतर तरीका (टेम्पलेट का उदाहरण बनाकर की जैसे किसी भी लाभ) है।

उत्तर

64

कुछ ओवरहेड पार्सिंग टेम्पलेट है। यदि आपका टेम्पलेट बड़ा है और आप इसे बार-बार उपयोग करते हैं तो आप टेम्पलेट को प्री-पार्स करके कुछ प्रदर्शन लाभ देख सकते हैं। आप कुछ इस तरह कर सकते हैं,

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

तो फिर तुम template.merge() बार बार यह हर पार्स करने के बिना कॉल कर सकते हैं।

बीटीडब्ल्यू, आप सीधे स्ट्रिंग को Velocity.evaluate() पर पास कर सकते हैं।

+0

मैं वास्तव में क्या देख रहा था। धन्यवाद। अन्य लोगों के संदर्भ के लिए, रनटाइम सर्विसेज org.apache.velocity.runtime का एक उदाहरण है। रनटाइंस्टेंस – tomsame

+0

मिस्ड एक-पंक्ति। पूर्णता के लिए, मैंने इसे जोड़ा। Velocity.Evalu के उल्लेख के लिए –

+12

+1 क्योंकि यह वही है जो मैं ढूंढ रहा था। –

10

उपर्युक्त नमूना कोड मेरे लिए काम कर रहा है। यह वेग संस्करण 1.7 और log4j का उपयोग करता है।

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

similar इतना सवाल।

+0

वाह, यह एक अद्वितीय दृष्टिकोण है। कैसे वेग इंजन लोड संसाधन आंतरिक कार्यान्वयन लगता है, संपत्ति "string.resource.loader.class" परिवर्तन के अधीन हो सकता है? –

0

वेग 2 JSR223 Java Scripting Language Framework जो स्ट्रिंग टेम्पलेट के रूप में बदलने के लिए एक और विकल्प बनाने में एकीकृत किया जा सकता है:

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); 
संबंधित मुद्दे