2010-07-01 9 views
15

एक .jsp में मैं का प्रयोग करेंगे:वसंत एमवीसी में freemarker के साथ संदेशों का उपयोग कैसे करें?

<fmt:message key="welcome.title"/> 

मेरी messages.properties फ़ाइल से एक संदेश प्रदर्शित करने के लिए।

मैं फ्रीमार्कर के साथ ऐसा कैसे करूं?

उत्तर

25

आयात स्प्रिंग मैक्रो

<#import "/spring.ftl" as spring/> 

फिर

<@spring.message "yourMessageKeyGoesHere"/> 

लेकिन आप ResourceBundleMessageSource रजिस्टर करने की आवश्यकता

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="messages"/> 
</bean> 

मन MessageSource रखें बुलाया जाना चाहिए messageSource

+1

इसलिए प्रत्येक फ्रीमार्कर टेम्पलेट को आयात करना है? – Blankman

+0

@ ब्लैंकमैन मुझे यकीन नहीं है लेकिन मुझे लगता है कि –

+0

जैसा कि मुझे याद है, आप डिफ़ॉल्ट रूप से टेम्पलेट आयात कर सकते हैं। @ ब्लैंकमैन: आप फ्रीमार्कर दस्तावेज़ों का उल्लेख कर सकते हैं। –

12

@Blankman

नहीं, आप प्रत्येक टेम्पलेट में मैन्युअल रूप से यह आयात करने के लिए नहीं है। जैसा कि नीचे दिखाया गया है, आप अपनी freemarker सेटिंग्स में auto_import प्रॉपर्टी सेट कर सकते हैं।

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
    ... 

    <property name="freemarkerSettings"> 
     <props> 
      <prop key="auto_import">spring.ftl as spring</prop> 
     </props> 
    </property> 
</bean> 
+1

मैंने ऑटो-आयात के माध्यम से spring.ftl के लिए यह कोशिश की, यह 'java.io.FileNotFoundException: टेम्पलेट spring.ftl नहीं मिला' –

+1

वसंत के रूप में /spring.ftl /spring.ftl में बदलें

1

अन्य अच्छे जवाब हैं। जावा कॉन्फ़िगरेशन को उन लोगों के लिए उदाहरण प्रदान करें जो इसका उपयोग करते हैं।

@Bean(name = "freemarkerConfig") 
public FreeMarkerConfigurer freemarkerConfig() { 
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); 
    configurer.setTemplateLoaderPaths("/WEB-INF/views/", 'classpath:/templates'); 
    Map<String, Object> map = new HashMap<>(); 
    map.put("xml_escape", new XmlEscape()); 
    configurer.setFreemarkerVariables(map) 
    def settings = new Properties() 
    settings['auto_import'] = 'spring.ftl as spring,layout/application.ftl as l,/macros/meh.ftl as meh' 
    configurer.setFreemarkerSettings(settings) 
    log.info "returning freemarker config" 
    return configurer; 
} 
संबंधित मुद्दे