首页 技术 正文
技术 2022年11月14日
0 收藏 409 点赞 3,407 浏览 2566 个字

freemarker入门实例

1、设计思路

(1)新建Maven Project

(2)生成freemarker模板

(3)写freemarker页面ftl文件

(4)写测试文件

2、新建Maven Project

3、生成freemarker模板

FreemarkerTemplate.java:

/** * @Title:FreemarkerTemplate.java * @Package:com.you.freemarker * @Description:freemarker模板 * @author:Youhaidong(游海东) * @date:2014-5-25 下午11:18:00 * @version V1.0 */package com.you.freemarker;import java.io.IOException;import java.io.PrintWriter;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;/** * 类功能说明 * 类修改者 修改日期 * 修改说明 * <p>Title:FreemarkerTemplate.java</p> * <p>Description:游海东个人开发</p> * <p>Copyright:Copyright(c)2013</p> * @author:游海东 * @date:2014-5-25 下午11:18:00 * @version V1.0 */public class FreemarkerTemplate{  /**   * 生成freemarker模板文件   * @Title:getTemplate   * @Description:   * @param:@param name   * @param:@return   * @return:Template   * @throws   */      public Template getTemplate(String name)      {      try      {      //通过FreeMarker的Configuration读取相应的FTL      Configuration conf = new Configuration();      //设置去哪里读取相应的ftl模板文件          conf.setClassForTemplateLoading(this.getClass(), "/com/you/ftl");          //在模板文件目录中找到名称为name的文件          Template temp = conf.getTemplate(name);          return temp;  }      catch (IOException e)  {  e.printStackTrace();  }return null;      }      /**       * 将结果输出到控制台       * @Title:printFtl       * @Description:       * @param:@param name       * @param:@param root       * @return: void       * @throws       */      public void printFtl(String name,Map<String,Object> root)      {      try      {      //通过Template可以将模板文件输出到相应的流      Template temp = this.getTemplate(name);      temp.process(root, new PrintWriter(System.out));  }      catch (TemplateException e)  {      e.printStackTrace();  }      catch (IOException e)  {      e.printStackTrace();  }      }}

4、写freemarker页面ftl文件

user.ftl:

姓名:${username}年龄:${age}性别:${sex}

5、写测试文件

FreemarkerTest.java:

/** * @Title:FreemarkerTest.java * @Package:com.you.test.freemarker * @Description:Freemarker测试 * @author:Youhaidong(游海东) * @date:2014-5-25 下午11:32:15 * @version V1.0 */package com.you.test.freemarker;import java.util.HashMap;import java.util.Map;import org.junit.Before;import org.junit.Test;import com.you.freemarker.FreemarkerTemplate;/** * 类功能说明 * 类修改者 修改日期 * 修改说明 * <p>Title:FreemarkerTest.java</p> * <p>Description:游海东个人开发</p> * <p>Copyright:Copyright(c)2013</p> * @author:游海东 * @date:2014-5-25 下午11:32:15 * @version V1.0 */public class FreemarkerTest{FreemarkerTemplate ft;/** * * @Title:buildUp * @Description:实例化对象 * @param: * @return: void * @throws */@Beforepublic void buildUp(){ft = new FreemarkerTemplate();}/** * * @Title:testFreemarker * @Description:测试结果 * @param: * @return: void * @throws */@Testpublic void testFreemarker(){//创建数据模型Map<String,Object> root = new HashMap<String,Object>();//为数据模型添加值root.put("username", "张三");root.put("age", "22");root.put("sex", "男");//将数据模型和模板中的数据输出到控制台ft.printFtl("user.ftl", root);}}

6、测试结果

姓名:张三年龄:22性别:男
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905