public class Student {
private Name name;
private String no;
private String sex;
private Integer age;
private Date entranceDate;
private boolean local;
public boolean isLocal() {
return local;
}
public void setLocal(boolean local) {
this.local = local;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getEntranceDate() {
return entranceDate;
}
public void setEntranceDate(Date entranceDate) {
this.entranceDate = entranceDate;
}
}
public class Name {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class FreeMarkerApp {
public static void main(String[] args) {
Configuration cfg = new Configuration();
Writer out = null;
try {
cfg
.setDirectoryForTemplateLoading(new File(
"/home/lingshangwen/repo/eclipse/freemarker/src/main/resources/templates"));
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template tmp = cfg.getTemplate("simple.ftl");
Map root = new HashMap();
List<Student> studentList = new ArrayList<Student>();
Student student = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date entranceDate = sdf.parse("2004-09-10");
Name name = null;
for (int i = 0; i < 10; i++) {
name = new Name();
name.setFirstName("eagle" + i);
name.setLastName("paraidse" + i);
student = new Student();
student.setName(name);
student.setAge(24);
student.setNo(String.valueOf(20040900 + i));
student.setEntranceDate(entranceDate);
if (i < 5) {
student.setLocal(true);
student.setSex("男");
} else {
student.setLocal(false);
student.setSex("女");
}
studentList.add(student);
}
root.put("studentList", studentList);
out = new OutputStreamWriter(System.out);
tmp.process(root, out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
simple.ftl
<#if studentList?exists>
<#list studentList as student>
${student.name.firstName}.${student.name.lastName} ${student.no} ${student.sex} ${student.age?c} ${student.local?string("本地学生", "外地学生")}
</#list>
</#if>
输出
eagle0.paraidse0 20040900 男 24 本地学生
eagle1.paraidse1 20040901 男 24 本地学生
eagle2.paraidse2 20040902 男 24 本地学生
eagle3.paraidse3 20040903 男 24 本地学生
eagle4.paraidse4 20040904 男 24 本地学生
eagle5.paraidse5 20040905 女 24 外地学生
eagle6.paraidse6 20040906 女 24 外地学生
eagle7.paraidse7 20040907 女 24 外地学生
eagle8.paraidse8 20040908 女 24 外地学生
eagle9.paraidse9 20040909 女 24 外地学生
分享到:
相关推荐
springboot如何使用Freemarker模版引擎
Springboot项目中: 1. 使用Apache POI 3.9 自定义样式导出Excel文件...2. 使用freemarker动态生成word .doc文档(带图片Word以及复杂格式word) 详细说明见个人博客及 github: https://github.com/DuebassLei/excel-poi
在struts2中使用freemarker模版.doc
1.html模板+model数据,通过freemarker进行渲染,便于维护和修改 2.渲染后的html流,可通过Flying Saucer组件生成pdf文件流,或者生成pdf后再转成jpg文件流
Freemarker页面语法 A 概念 最常用的 3 个概念 sequence 序列,对应java 里的list 、数组等非键值对的集合 hash 键值对的集合 namespace 对一个ftl 文件的引用, 利用这个名字可以访问到该ftl 文件的资源 ...
freemarker是一个比较好用的java模板引擎,grails也对它有很好的支持,该资源大有用处。
使用freemarker 导出word供别人下载
Spring配置Freemarker视图解析器,前台页面全部使用freemarker渲染
使用FreeMarker实现到处word实例 。
NULL 博文链接:https://cdxs2.iteye.com/blog/1923067
使用freemarker生成word ,并集成struts2 同时生成及下载文档 资料附有Java源代码和自己总结的使用说明及注意事项 大至预览如下: 1、用word编辑好模板 普通字符串替换为 ${string} 表格循环用标签 姓名:${...
NULL 博文链接:https://gznofeng.iteye.com/blog/251325
使用freeMarker模板和Spring boot后端API接口实现查询用户列表的功能。 http://localhost:8080/ftl/index
简单的实现了java使用freemarker模板技术导出word的简单功能,不喜勿喷,网上很多的不详细
itext-pdf使用freemarker模板生成pdf时,样式错乱,乱折行问题解决,重写源码
java 将html转为word,使用Freemarker 比较好装好。方便好用。也可以转换成pdf格式。
使用freemarker生成复杂的word文档,生成的文档支持office查看,但是使用国产中标普华貌似有问题,office、wps没有问题。
java导出word使用freemarker模板map数据导出word带图片 可以循环map 中list。 调用的2种方法在博客 https://blog.csdn.net/yjflinchong/article/details/122614203
java使用Freemarker生成PDF以及HTML等文档.zip
使用FreeMarker配合Servlet写了一个分页程序,使用环境是Java7和Tomcat7,Eclipse 4.2,程序比较简单,并且加了注释,应该算是清楚易懂吧,适合初学者(我本身也是初学者),有问题私信可以再和大家讨论。