利用FreeMaker生成HTML文件(或其他格式)- Java

开发环境

freemaker版本2.3.23
Intellj Idea

在Idea上创建一个Meven项目

在idea上new一个项目,配置sdk,jdk,命名为demo(随意命名)

在pom.xml上导入依赖(导入后点击import changes即可自动加载)

		<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>

开发步骤

1. 创建或获取一个.ftl模版文件

	在项目目录target/classes下新建一个文件夹static,新建一个文件test.ftl,写入下列代码
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; cjarse="utf-8">
		<title>Html Test</title>
	</head>
	<body>
		<div>
			${content!null}
		</div>
	</body>
</html>

2. 创建一个Configuration对象,直接new一个对象。构造方法对应freemaker版本号

 			//创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
 			Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

3. 设置模版文件所在的路径

 			//创建模版所在路径
            ClassPathResource resource = new ClassPathResource("/static/ftl");
            cfg.setDirectoryForTemplateLoading(new File(resource.getURI()));

4.设置模版编码格式(一般为utf-8)

 			//设置模版编码格式
            cfg.setDefaultEncoding("utf-8");

5.加载一个模版,创建一个模版对象

			//获取模版
            Template temp = cfg.getTemplate("test.ftl");

6.创建一个模版所用的数据集(一般是Map),并向其中添加数据

			//创建一个模块使用的数据集,一般是map
            Map<String,Object> root = new HashMap<>();
            //向数据集中添加数据
            root.put("content",content);

7.创建一个Write对象(一般创建FileWrite对象),指定生成文件名

 			//创建一个Write对象,指定生成文件名
            Writer out = new FileWriter(new File("D:\\demo\\target\\classes\\static\\html\\"+name));

8.调用模块对象的process方法,输出文件

			//调用模块对象的process方法输出文件
            temp.process(root, out);

9.关闭流

			//关闭流
            out.close();

控制类中的完整代码

public String freeMakerContent(String content){
        //创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSS");
            Date date = new Date();
            String time = format.format(date);
            String name = time + ".html";
            //创建模版所在路径
            ClassPathResource resource = new ClassPathResource("/static/ftl");
            cfg.setDirectoryForTemplateLoading(new File(resource.getURI()));
            //设置模版编码格式
            cfg.setDefaultEncoding("utf-8");
            //获取模版
            Template temp = cfg.getTemplate("test.ftl");
            //创建一个模块使用的数据集,一般是map
            Map<String,Object> root = new HashMap<>();
            //向数据集中添加数据
            root.put("content",content);
            //创建一个Write对象,指定生成文件名
            Writer out = new FileWriter(new File("D:\\demo\\target\\classes\\static\\html\\"+name));
            //调用模块对象的process方法输出文件
            temp.process(root, out);
            //关闭流
            out.close();
            return name;
        }catch (IOException e){
            e.printStackTrace();
        }catch (TemplateException e){
            e.printStackTrace();
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值