模板引擎freemarker的简单使用教程

freemarker十分强大,而且不依赖web容器,个人感觉十分好用。

下面直接进主题,freemarker还有什么特性,请找度娘或谷哥~

一、freemarker生成word

1.创建模板。

我创建模板的方法比较简单,也不知道有没有其他更好的方法,有的话,请告诉我吧~

首先是新建一个word文档,按照内容格式排好版,然后在需要注入信息的位置先写上占位置的数据,如图1,然后另存为xml文件(我是存为2003版本的xml),然后用文本编辑器把xml打开,在xml中把对应的数据改为freemarker的输出表达式,如图2,然后保存,把xml的后缀名改为freemarker的文件后缀名ftl,便是一个freemarker模板了。

图1:

图2:

2.实现程序

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * 使用freemark生成word
 *
 */
public class Freemark {

    public static void main(String[] args){
        Freemark freemark = new Freemark("template/");
        freemark.setTemplateName("wordTemplate.ftl");
        freemark.setFileName("doc_"+new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date())+".doc");
        freemark.setFilePath("bin\\doc\\");
        //生成word
        freemark.createWord();
    }

    private void createWord(){

        Template t = null;
        try {
            //获取模板信息
            t = configuration.getTemplate(templateName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        File outFile = new File(filePath+fileName);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Map map = new HashMap<String, Object>();
        map.put("name", "蒙奇·D·路飞");
        map.put("country", "日本");
        map.put("city", "东京");
        map.put("time",new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date()));
        try {
            //输出数据到模板中,生成文件。
            t.process(map, out);
            out.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * freemark初始化
     * @param templatePath 模板文件位置
     */
    public Freemark(String templatePath) {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(this.getClass(),templatePath);
    }
    /**
     * freemark模板配置
     */
    private Configuration configuration;
    /**
     * freemark模板的名字
     */
    private String templateName;
    /**
     * 生成文件名
     */
    private String fileName;
    /**
     * 生成文件路径
     */
    private String filePath;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getTemplateName() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }

}

3.程序运行后,会在bin的doc目录下生成doc文件,效果图

4.demo源码下载:http://download.csdn.net/detail/stormwy/7370997

因为Demo比较小,也可以通过图包的方式下载,把下图下载改名解压即可:

本文转自:http://blog.csdn.net/stormwy/article/details/26172353

时间: 2024-08-27 10:23:09

模板引擎freemarker的简单使用教程的相关文章

Java模板引擎 FreeMarker

概念 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写. 它是为Java程序猿提供的一个开发包.它不是面向终于用户的,而是为程序猿提供的一款能够嵌入他们所开发产品的应用程序. 介绍 那么.FreeMarker是一款如何的工具呢?FreeMarker实际上是被设计用来生成HTML Web页面,尤其是通过实现了基于MVC模式的Java Servlet应用程序.使用MVC模式的动态页面的设计构思使得你能够将前端设计师从程序猿中分离出来.全部人各司其职,发挥其最

模板引擎Thymeleaf的简单了解

模板引擎 SpringBoot中,项目以jar包的形式打包,而不是web项目,而且使用的tomcat是嵌入式的tomcat,故不支持JSP了 模板引擎:JSP.Velocity.Freemarker.Thymeleaf SpringBoot推荐的Thymeleaf: 语法更简单,功能更强大: 1.引入thymeleaf: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>

Spring Boot基础6-web应用开发-模板引擎FreeMarker

原文视频参考:http://www.roncoo.com/course/view/c99516ea604d4053908c1768d6deee3d 一.spring boot的web应用开发,是基于spring mvc 二.Spring boot 在spring默认基础上,自动配置添加了以下特性: 1.   包含了ContentNegotiatingViewResolver和BeanNameViewResolver beans. 2.   对静态资源的支持,包括对WebJars的支持. 3.  

springBoot(5):web开发-模板引擎FreeMarker

一.简介 spring boot的web应用开发,是基于spring mvc. Spring boot在spring默认基础上,自动配置添加了以下特性: 1.包含了ContentNegotiatingViewResolver和BeanNameViewResolver beans. 2.对静态资源的支持,包括对WebJars的支持. 3.自动注册Converter,GenericConverter,Formatter beans. 4.对HttpMessageConverters的支持. 5.自动

springboot中添加模板引擎freemarker和thymeleaf

freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 <!-- 集成freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</ar

nodejs 模板引擎ejs的简单使用(2)

test.ejs <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <% for(var i=0;i<json.arr.length;i++){ %> <div>用户名:<%=json.arr[i].user%> 密码:<%=j

nodejs 模板引擎ejs的简单使用(3)

1.ejs <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <% for(var i=0;i<2;i++){ %> <% include ../a.txt %> <% } %> </body> </html>

nodejs 模板引擎jade的简单使用

1.jade html head style script body div ul li li jade1.js var jade=require('jade'); var str=jade.renderFile('./view/1.jade',{pretty:true}); console.log(str); 项目部署: 输出结果:

PHP自写简单模板引擎,供新手学习

今天要写点小东西,突然想到要用模板引擎.就"随手"写了一个,发上来供新手学习.搞了这么久PHP,想想也真是感慨,当年研究了几天的东西现在一两个小时就弄完了,当年一起完耍的女神现在已经不知所踪了...咳咳,下面是代码: <?php /**************** * @author: 一曲忧伤 * @email: [email protected] * @discription: 简单模板引擎 */ class view { var $tpl_dir = 'template';