Freemarker应用案例

本文介绍了Freemarker模板引擎的使用,包括下载和在Java Web项目中的应用,阐述了如何将Freemarker与HTML结合生成动态网页。
摘要由CSDN通过智能技术生成
package entity;

public class User {
	private String name;
	private String passwd;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
}
核心代码:
package utit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

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

public class FreeMarkertUtil {
	/**
	 * templatePath模板文件存放路径,templateName 模板文件名称,filename 生成的文件名称
	 * @param templatePath
	 * @param templateName
	 * @param fileName
	 * @param root
	 */
	public static void analysisTemplate(String templatePath,
			String templateName, String fileName, Map<?, ?> root) {
		try {
			//初使化FreeMarker配置
			Configuration config = new Configuration();
			// 设置要解析的模板所在的目录,并加载模板文件
			config.setDirectoryForTemplateLoading(new File(templatePath));
			// 设置包装器,并将对象包装为数据模型
			config.setObjectWrapper(new DefaultObjectWrapper());

			// 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
			// 否则会出现乱码
			Template template = config.getTemplate(templateName, "UTF-8");
			// 合并数据模型与模板
			FileOutputStream fos = new FileOutputStream(fileName);
			Writer out = new OutputStreamWriter(fos, "UTF-8");
			template.process(root, out);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

}

调用:

package client;

import java.util.HashMap;
import java.util.Map;

import utit.FreeMarkertUtil;

import entity.User;

public class CreateHtml {
	public static void main(String[] args) {
		User user = new User();
		user.setName("张三");
		user.setPasswd("000");

		Map<String, Object> root = new HashMap<String, Object>();
		root.put("user", user);
		String templatesPath = "D:/JAVA/MyEclipse/myeclipse.10/Workspaces/MyEclipse 10/createHtml/WebRoot/WEB-INF/";
		String templateFile = "staticHtml.ftl";
		String htmlFile = templatesPath + "userhtml.html";
		FreeMarkertUtil.analysisTemplate(templatesPath, templateFile, htmlFile,
				root);
	}
}

ftl文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>user.ftl</title>
</head>
  <body>
	用户名:${user.name}<br/>
	密码:${user.passwd}
  </body>
</html>

Freemarker-jar包

Freemarker下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值