Java freemarker + spring 配置和最简单的例子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28960211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
freemarker + spring configuration and simplest example
提问by Mykhaylo Adamovych
Despite there are a lot of discussion around freemarker + spring but it is hard to find neat working example to copy and run.
Could you please provide simplest working configuration of freemarker in spring xml context and java code snippet to load template from resource file and process it.
尽管有很多关于 freemarker + spring 的讨论,但很难找到简洁的工作示例来复制和运行。
您能否在 spring xml 上下文和 java 代码片段中提供最简单的 freemarker 工作配置,以从资源文件加载模板并对其进行处理。
采纳答案by Mykhaylo Adamovych
pom.xml
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
applicationContext.xml
应用上下文.xml
<bean id="freeMarkerConfigurationFactory" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="templateLoaderPath" value="classpath:/META-INF/freemarker"/>
<property name="preferFileSystemAccess" value="false"/>
</bean>
AlertMailComposer.java
AlertMailComposer.java
import static org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString;
@Component
public class AlertMailComposer implements Processor {
public static final String TEMPLATE = "AlertMail.ftl";
@Autowired
private Configuration freemarkerConfiguration;
protected String composeHtml(Alert alert) throws IOException, TemplateException {
return processTemplateIntoString(freemarkerConfiguration.getTemplate(TEMPLATE), ImmutableMap.of(
"alertType", alert.getAlertType(),
"message", alert.getMessage(),
"nodeName", alert.getEvent().getNodeName(),
"event", toJson(alert.getEvent(), true)
));
}
...
AlertMail.ftl
警报邮件.ftl
<html>
<body style="font-family:verdana;font-size:10">
<b>${alertType}: </b>${message}<br>
<b>on: </b>${nodeName}<br>
<p/>
<pre style="font-family:verdana;font-size:10;color:grey">
${event}
</pre>
</body>
</html>
回答by lfo
In the spring context xml, declaring FreemarkerConfigurationFactoryBean
is sufficient, i.e.
在spring上下文xml中,声明FreemarkerConfigurationFactoryBean
就足够了,即
<bean id="freemarkerConfigFactory" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="templateLoaderPath" value="classpath:templates/"/>
</bean>
No need to further specify freemarker.template.Configuration
bean in the xml file if you use @Autowired
annotation. It is created by the factory and injected by Spring.
freemarker.template.Configuration
如果使用@Autowired
注解,则无需在 xml 文件中进一步指定bean 。它由工厂创建并由 Spring 注入。
回答by Aniket Thakur
Other than maven dependency in pom.xml to use it with java configuration in spring you can do -
除了 pom.xml 中的 maven 依赖以在 spring 中将其与 java 配置一起使用,您还可以执行以下操作:
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { MyRootContextConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { MyServletContextConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/test/*" };
}
}
and then MyRootContextConfig
can have
然后MyRootContextConfig
可以有
@Bean(name = "myFreeMarkerConfigruation")
public FreeMarkerConfigurationFactoryBean getFreeMarkerConfiguration() {
FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
bean.setTemplateLoaderPath("classpath:/templates/");
return bean;
}