2008-10-23 16 views
18

मेरे स्प्रिंग ऐप में, मैं अपने आवेदन द्वारा भेजे जाने वाले ईमेल के टेक्स्ट जेनरेट करने के लिए फ्रीमार्कर का उपयोग करना चाहता हूं। जेनरेट किया गया टेक्स्ट कभी भी दृश्य में वापस नहीं किया जाएगा, इसलिए मुझे फ्रीमार्कर व्यू रिज़ॉल्वर को कॉन्फ़िगर करने की आवश्यकता नहीं है। प्रलेखन संकेत मिलता है कि मैं इसस्प्रिंग ऐप में फ्रीमार्कर से टेम्पलेट टेक्स्ट प्राप्त करना

<bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> 
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/> 
</bean> 

की तरह एक FreeMarkerConfigurationFactoryBean कॉन्फ़िगर करना चाहिए एक बार मैं इस सेम कॉन्फ़िगर किया गया है कि कैसे मैं वास्तव में चर का एक विशेष नक्शा के साथ, पाठ है कि एक विशेष टेम्पलेट के लिए उत्पन्न होता है मिलता है है लगता है।

String templateName = "email" 
Map templateVars = new HashMap(); 
templateVars.put("firstName", "john"); 
templateVars.put("surname", "doe");  
// Now how do I get the template text? 

Spring modules जो खाका भी बहुत स्पष्ट पुन: प्राप्त करने में आता है वसंत और FreeMarker के बीच एक विकल्प के एकीकरण प्रदान करने के लिए लगता है, लेकिन मैं अपने अनुप्रयोग के लिए एक अतिरिक्त निर्भरता को जोड़ने के लिए नहीं करना चाहते हैं: दूसरे शब्दों में, क्या कोड के बाद आता है जब तक कि यह बिल्कुल जरूरी नहीं है।

साथ ही, क्या मुझे यह सुनिश्चित करने के लिए फ्रीमार्कर कॉन्फ़िगरेशन फैक्ट्रीबीन में कुछ अतिरिक्त कॉन्फ़िगरेशन जोड़ने की आवश्यकता है ताकि टेम्पलेट कैश किए गए हों?

चीयर्स, डॉन

उत्तर

22

कुछ इस तरह

काम करना चाहिए कोड आपके द्वारा दी गई करने से पहले, प्रारंभ:

MailSender mailSender = new JavaMailSenderImpl(); 
SimpleMailMessage message = new SimpleMailMessage(); 

फिर, अपने कोड के बाद, जोड़ें:

StringBuffer content = new StringBuffer(); 
try { 
    content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
     configuration.getTemplate(templateName), templateVars)); 
} catch (IOException e) { 
    // handle 
} catch (TemplateException e) { 
    // handle 
} 

message.setFrom(getMailFromName() + " <" + getMailFromAddr() + ">"); 
message.setTo(getMailTo()); 
if (getCcTo() != null) 
    message.setCc(getCcTo()); 
message.setSubject(getSubject()); 
message.setText(content.toString()); 

mailSender.send(message); 

यहां मेरा आवेदन है Context.xml:

<bean id="freemarkerMailConfiguration" 
    class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> 
    <property name="templateLoaderPath" value="/WEB-INF" /> 
</bean> 
<bean id="yourEmailServiceClass" class="YourEmailServiceClass"> 
    <property name="mailSender" ref="mailSender" /> 
    <property name="freemarkerMailConfiguration" ref="freemarkerMailConfiguration" /> 
    <property name="freemarkerTemplate" value="email.ftl" /> 
    <property name="mailFromName" value="John Q. Programmer" /> 
    <property name="mailFromAddr" value="[email protected]" /> 
    <property name="subject" value="Email Subject" /> 
</bean> 

और अपने कैशिंग सवाल ...

मैं सिर्फ एक 'viewResolver' सेम, जो आप ने कहा कि आप का उपयोग नहीं करने में एक सेम संपत्ति 'कैश' देखा है।

यह भी देखें: Chapter 14. Integrating view technologies

+0

धन्यवाद, शायद चर आप विन्यास नाम दे देते हैं सेम मैं freemarkerConfiguration नामित किया गया है? –

+0

हां, हमने जिस वर्ग में उपयोग किया था, उसमें 'कॉन्फ़िगरेशन' चर 'कॉन्फ़िगरेशन' प्रकार है। मेरी संशोधित पोस्ट देखें ^^ –

+0

अभी भी कोई कॉन्फ़िगरेशन परिभाषा नहीं है। –

संबंधित मुद्दे