2010-07-01 66 views

回答

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

因此每個freemarker模板都必須導入? – Blankman 2010-07-01 03:48:42

+0

@Blankman我不確定,但我認爲如此 – 2010-07-01 03:55:12

+0

據我所知,您可以默認導入模板。 @Blankman:你可以參考Freemarker文檔。 – 2011-09-08 09:16:30

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:Template spring.ftl not found' – 2013-02-22 07:08:23

+1

更改爲/spring.ftl /spring.ftl作爲spring 2014-04-25 05:54:10

1

其他人都很好的答案。提供java config作爲例子,用於那些使用它。

@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; 
} 
相關問題