(五)Freemarker 整合Spring

来源:互联网 发布:排序算法及时间复杂度 编辑:程序博客网 时间:2024/05/17 03:20

 Freemarker 可以和spring 进行无缝整合, 整合方式也非常简单。 Freemarker 在Web 应用中发送邮件中,做邮件模板是一个非常好的一个应用方式。

【1. 引入jar 包】

   除了添加spring 的jar包以外,还需要添加 freemarker 的jar 包

【2. 添加配置】

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.   
  6.     <!-- 配置spring 的freemarker引擎  -->  
  7.     <bean id="springFreemarkerCfg"  
  8.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  9.         <property name="templateLoaderPath" value="classpath:/templates" />  
  10.         <property name="freemarkerSettings">  
  11.             <props>  
  12.                 <prop key="template_update_delay">0</prop>  
  13.                 <prop key="default_encoding">UTF-8</prop>  
  14.             </props>  
  15.         </property>  
  16.     </bean>  
  17.   
  18. </beans>  

【3. 测试用例】

[java] view plain copy
  1. package org.zgf.learn.freemarker.spring;  
  2.   
  3. import java.io.StringWriter;  
  4. import java.util.Date;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import org.junit.Test;  
  9. import org.junit.runner.RunWith;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11. import org.springframework.test.context.ContextConfiguration;  
  12. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  13. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;  
  14.   
  15. import freemarker.template.Configuration;  
  16. import freemarker.template.Template;  
  17.   
  18. @RunWith(SpringJUnit4ClassRunner.class)  
  19. @ContextConfiguration(locations="classpath:org/zgf/learn/freemarker/spring/applications-freemarker.xml")  
  20. public class Test_spring_freemarker {  
  21.       
  22.     //1. 获取spring 的freeMarkerConfigurer 配置类  
  23.     @Autowired  
  24.     private FreeMarkerConfigurer springFreemarkerCfg;  
  25.   
  26.     //Freemarker 原生的配置类型  
  27.     private Configuration freeMarkerCfg;  
  28.       
  29.     @Test  
  30.     public void test() throws Exception{  
  31.         //2. 获取 freemarker:通过sprign 的FreeMarkerConfigurer Bean 来创建  
  32.         freeMarkerCfg = springFreemarkerCfg.getConfiguration();  
  33.           
  34.         //3. 加载模板  
  35.         Template template = freeMarkerCfg.getTemplate("spring.freemarker.ftl");  
  36.           
  37.         //4. 构造数据对象   
  38.         Map<String,Object> root = new HashMap<>();  
  39.         root.put("time"new Date());  
  40.           
  41.         //5. 输出   
  42.         StringWriter writer = new StringWriter();  
  43.         template.process(root, writer);  
  44.         String content = writer.getBuffer().toString();  
  45.           
  46.         System.out.println("content:" + content);  
  47.     }  
  48. }  
原创粉丝点击