FreeMarker、JAVA技术
2016-06-14 17:35
579 查看
JAVA类
FreeMarker .java
package cn.com.freemarker; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class FreeMarker { public static void main(String[] args) { //创建配置实例Cofiguration Configuration cfg = new Configuration(); try { //工程目录下的目录(template在工程下)--推荐 cfg.setDirectoryForTemplateLoading(new File("template")); //获取模板(template) Template template = cfg.getTemplate("jump.ftl"); //建立数据模型(Map) Map<String, String> map = new HashMap<String, String>(); map.put("name", "wutz"); map.put("age", "25"); File homeIndex = new File("template/index.html"); Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(homeIndex), "UTF-8")); template.process(map, writer); writer.flush(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TemplateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
模板
jump.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>FreeMarker</title> </head> <body> name : ${name} age : ${age} </body> </html>
生成页面
index.html
<!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>FreeMarker</title> </head> <body> name : wutz age : 25 </body> </html>
相关文章推荐
- freemarker 数字格式化深入理解
- Java操作FreeMarker模板引擎的基本用法示例小结
- 使用Java进行FreeMarker的web模板开发的基础教程
- freemarker判断对象是否为空的方法
- java Spring整合Freemarker的详细步骤
- Freemarker 最简单的例子程序
- Freemarker常用指令使用示例
- Java模版引擎Freemarker
- 基于Java的Spring框架来操作FreeMarker模板的示例
- 使用Freesql简单地实现多条件查询
- JFinal使用Freemarker作为展示层中的国际化配置使用
- Spring+mybatis+shiro+freemarker+ehcache+ldap+mongo
- FreeMaker 时间的比较
- superword中的模板抽取实践
- Freemarker输出$和html标签等特殊符号
- 在FTL文件中使用自定义的Java方法
- Freemarker 自定义标签 实现TemplateDirectiveModel
- FreeMarker在eclipse中的入门例子
- Freemarker中如何遍历List
- jFinal开发规范