[英]how to use messages with freemarker in spring mvc?
在.jsp中我会使用:
<fmt:message key="welcome.title"/>
从我的messages.properties文件中显示一条消息。
我如何用freemarker做到这一点?
导入Spring宏
<#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
@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>
其他人都很好。 提供java配置作为使用它的人的例子。
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.