java framemaker教程_Freemarker入门案例

本文介绍如何使用Freemarker模板引擎创建模板文件,并通过Java代码实现数据填充及输出。包含Freemarker工具类的编写、模板文件的结构示例以及JUnit测试验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先需要到freemarker官方下载freemarker的jar包,导入到项目中,如:freemarker-2.3.19.jar

8581bc262cc5395fc1e86812dfb9a87a.png

1、先建个freemarker的工具类,FreemarkerUtil.java

packagecom.ljq.fm;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Map;importfreemarker.template.Configuration;importfreemarker.template.Template;importfreemarker.template.TemplateException;public classFreemarkerUtil {publicTemplate getTemplate(String name) {try{//通过Freemaker的Configuration读取相应的ftl

Configuration cfg = newConfiguration();//设定去哪里读取相应的ftl模板文件

cfg.setClassForTemplateLoading(this.getClass(), "/ftl");//在模板文件目录中找到名称为name的文件

Template temp =cfg.getTemplate(name);returntemp;

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 控制台输出

*

*@paramname

*@paramroot*/

public void print(String name, Maproot) {try{//通过Template可以将模板文件输出到相应的流

Template temp = this.getTemplate(name);

temp.process(root,newPrintWriter(System.out));

}catch(TemplateException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}/*** 输出HTML文件

*

*@paramname

*@paramroot

*@paramoutFile*/

public void fprint(String name, Maproot, String outFile) {

FileWriter out= null;try{//通过一个文件输出流,就可以写到相应的文件中,此处用的是绝对路径

out = new FileWriter(new File("E:/workspace/freemarkprj/page/" +outFile));

Template temp= this.getTemplate(name);

temp.process(root, out);

}catch(IOException e) {

e.printStackTrace();

}catch(TemplateException e) {

e.printStackTrace();

}finally{try{if (out != null)

out.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

2 、在src目录下建个ftl包,用于存放ftl模板文件,this.getClass() 就是根据当前类的路径获取模板文件位置

01.ftl

测试

你好${username}

02.ftl

Insert title here

你好: ${username}

03.ftl

Insert title here

${user.id}-----${user.name}-----${user.age}

${user.name}还是一个小孩${user.name}快成年${user.name}已经成年#if>

04.ftl

Insert title here

${user.id}---------${user.name}-------${user.age}

#list>

05.ftl

Insert title here

${user.id}---------${user.name}-------${user.age}

#list>

06.ftl

Insert title here

${(user.group.name)!"1234"}

${(a.b)!"没有a.b元素"}

!:指定缺失变量的默认值??:判断某个变量是否存在,返回boolean值-->

不为空为空#if>

实体类User.java

packagecom.ljq.fm;importjava.io.Serializable;

@SuppressWarnings("serial")public class User implementsSerializable {private intid;privateString name;private intage;privateGroup group;publicGroup getGroup() {returngroup;

}public voidsetGroup(Group group) {this.group =group;

}publicUser() {

}public User(int id, String name, intage) {this.id =id;this.name =name;this.age =age;

}public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}

}

实体类Group.java

packagecom.ljq.fm;/***

*

*@author林计钦

*@version1.0 2013-10-25 下午02:36:09*/

public classGroup {privateString name;publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}

}

3、再建个Junit的测试类 FreemarkerTest.java

packagecom.ljq.fm;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Map;importfreemarker.template.Configuration;importfreemarker.template.Template;importfreemarker.template.TemplateException;public classFreemarkerUtil {publicTemplate getTemplate(String name) {try{//通过Freemaker的Configuration读取相应的ftl

Configuration cfg = newConfiguration();//设定去哪里读取相应的ftl模板文件

cfg.setClassForTemplateLoading(this.getClass(), "/ftl");//在模板文件目录中找到名称为name的文件

Template temp =cfg.getTemplate(name);returntemp;

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 控制台输出

*

*@paramname

*@paramroot*/

public void print(String name, Maproot) {try{//通过Template可以将模板文件输出到相应的流

Template temp = this.getTemplate(name);

temp.process(root,newPrintWriter(System.out));

}catch(TemplateException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}/*** 输出HTML文件

*

*@paramname

*@paramroot

*@paramoutFile*/

public void fprint(String name, Maproot, String outFile) {

FileWriter out= null;try{//通过一个文件输出流,就可以写到相应的文件中,此处用的是绝对路径

out = new FileWriter(new File("E:/workspace/freemarkprj/page/" +outFile));

Template temp= this.getTemplate(name);

temp.process(root, out);

}catch(IOException e) {

e.printStackTrace();

}catch(TemplateException e) {

e.printStackTrace();

}finally{try{if (out != null)

out.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值