freemarker实现动态生成内容

 

Freemarker作为一款非常优秀的模版框架,struts2默认集成,配置返回resulttype属性为freemarker,即将该返回的表现层指定为freemarker模版文件后缀为.ftl

当然,freemarker的魅力远远不止表现在这。

 

比如在实际开发中,会有各种各样的报文,假如报文的格式如下:

<?xml version="1.0" encoding="utf-8"?>

<commands>

<command>Im index on 0</command>

<command>Im index on 1</command>

<command>Im index on 2</command>

<command>Im index on 3</command>

<command>Im index on 4</command>

<command>Im index on 5</command>

<command>Im index on 6</command>

<command>Im index on 7</command>

<command>Im index on 8</command>

<command>Im index on 9</command>

</commands>

 

我们可以通过freemaker的api动态的生成我们想要的报文,只需要把原料给它,它就能给我们想要的东西,当然这也是模版框架意义.

下面给出代码:



package com.zhl.modules.fk.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class TemplateUtils {
	public static String mergeTemplateContent(Map<String, List<String>> values,String ftl){
		//定义字节数组输出流
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		//这是配置的核心类
		Configuration config = new Configuration();
		try {
			//设置模版存放目录
			config.setDirectoryForTemplateLoading(new File("WebContent/ftls"));
			//模版类
			Template tp = config.getTemplate(ftl+".ftl");
			//将变量与模版合并,并将合并后的内容给输出流
			tp.process(values, new OutputStreamWriter(output));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
		//返回输出流的字符串形式
		return output.toString();
	}
	
	public static void main(String[] args) {
		Map<String, List<String>> values = new HashMap<String, List<String>>();
		List<String> vals = new ArrayList<String>();
		for(int i = 0;i<10;i++){
			vals.add("Im index on "+i);
		}
		values.put("aValue", vals);
		//将最终结果打印出来
		System.out.println(mergeTemplateContent(values, "form"));
	}
}

模版文件form.ftl内容:

<?xml version="1.0" encoding="utf-8"?>

<commands>

<#list aValue as cm>

<command>${cm}</command>

</#list>

</commands>

 

 

执行我们的程序:

工程目录:

关于struts2部分,自行忽略


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架。它简化了基于Spring的应用程序的开发过程,提供了自动配置和约定大于配置的原则。LibreOffice是一个开源的办公套件软件,它提供了创建和编辑Word文档等功能。Freemarker是一个适用于Java平台的模板引擎,可以实现动态生成文本文件,比如docx文档。 在Spring Boot应用程序中使用LibreOffice和Freemarker动态生成docx文档的过程如下: 1. 在pom.xml文件中添加相关依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.libreoffice</groupId> <artifactId>libreoffice-java</artifactId> <version>1.1.0</version> </dependency> ``` 2. 创建一个Freemarker模板文件,用于定义生成docx文档的格式和内容。模板文件可以包含动态的变量,比如用户的姓名、日期等。 3. 在Spring Boot的主类中创建一个API接口,用于接收生成docx文档的请求。可以使用`@GetMapping`或`@PostMapping`注解指定API的路径。 4. 在API的方法中,使用Freemarker的模板引擎来动态生成docx文档。可以使用`Configuration`类来加载模板文件,使用`Template`类来渲染模板并生成文档内容。 5. 使用LibreOffice的Java API来将docx文档转换为其他文件格式,比如PDF。可以使用`OfficeManager`类来启动一个LibreOffice实例,使用`OfficeDocumentConverter`类来执行转换操作。 6. 将生成的docx文档保存到服务器指定的目录,并返回给客户端进行下载。 通过以上步骤,就可以在Spring Boot应用程序中利用Freemarker模板和LibreOffice实现动态生成docx文档的功能了。这样可以更加灵活和方便地生成各种格式的文档,并且可以自定义文档的内容和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值