天天看点

freemark什么是freemarkJavaApi 用法freemark模板的语法所有代码显示效果

什么是freemark

FreeMarker是一个用Java语言编写的模板引擎,它基于模板输出文本。FreeMarker与Web容器无关,即在Web运行时,它依赖于Servlet或HTTP。它不仅可以用作表现层(html)的实现技术,而且还可以用于生成XML,JSP或Java 等。

**一句话:**freemark就是根据 模板+数据 -->生成静态页面 通常模板用 .ftl作为后缀

**应用场景:**将热点变更频率少的页面静态化,加快访问速度。减轻服务器压力

JavaApi 用法

固定套路 共八步

@Test
	public void genHtml() throws IOException, TemplateException{
//		第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
		Configuration configuration = new Configuration(Configuration.getVersion());
		
//		第二步:设置模板文件所在的路径。  模板文件所在的上级文件夹
		configuration.setDirectoryForTemplateLoading(new 	   File("D:/src/main/resources/template"));

//		第三步:设置模板文件使用的字符集。一般就是utf-8.
		configuration.setDefaultEncoding("utf-8");
		
//		第四步:加载一个模板,创建一个模板对象。
		Template template = configuration.getTemplate("hello.htm");
		
//		第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
		Map map = new HashMap();
		map.put("hello", "hello周周周</br>");
		map.put("person", new Person(10001, "嬴政"));
		List<Person> list = new ArrayList<>();
		list.add(new Person(10002, "秦始皇"));
		list.add(new Person(10003, "荆轲"));
		list.add(new Person(10004, "貂蝉"));
		map.put("list", list);
		
		Map map2 = new HashMap<>();
		map2.put("m1", new Person(10005, "周杰伦"));
		map2.put("m2", new Person(10006, "蔡依林"));
		map2.put("m3", new Person(10007, "张惠妹"));
		
		map.put("mymap", map2);
		map.put("date", new Date());
		
//		map.put("test", "爱情是个什么东西?");
        
//		第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。可以任意后缀
		FileWriter writer = new FileWriter("D:/src/main/resources/template/zcm.html");
//		第七步:调用模板对象的process方法输出文件。
		template.process(map, writer);
//		第八步:关闭流。
		writer.close();
	}
           

在第二步和第四步指定下 已经定位到了模板位置

在第五步已经定义了 模板数据

模板 加 数据 == 静态页面

freemark模板的语法

创建hello.ftl 模板

key------value的形式

对于这个数据在模板引用形式为

访问pojo中的属性

对于这个数据在模板引用形式为

${person.id?c}----${person.name}   //?c  不显示千分位的,
           

取集合中的数据

List<Person> list = new ArrayList<>();
		list.add(new Person(10002, "秦始皇"));
		list.add(new Person(10003, "荆轲"));
		list.add(new Person(10004, "貂蝉"));
		map.put("list1", list);
           

对于这个数据在模板引用形式为

<#list list1 as item>
	${item.id?c}---
	${item.name}</br>
</#list>
           

取循环中的下标

<#list list1 as item>
	${item_index}
</#list>
           

取Map集合中的数据

Map map2 = new HashMap<>();
		map2.put("m1", new Person(10005, "周杰伦"));
		map2.put("m2", new Person(10006, "蔡依林"));
		map2.put("m3", new Person(10007, "张惠妹"));
		
		map.put("mymap", map2);
           

对于这个数据在模板引用形式为

<#list mymap?keys as key>
	${key}--
	${mymap[key].id?c}--
	${mymap[key].name}</br>
</#list>
           

效果

m1-- 10005-- 周杰伦
m2-- 10006-- 蔡依林
m3-- 10007-- 张惠妹
           

判断

//上文中的 list
<#list list as item>
	<#if item_index%2==0>
		这是偶数-->${item_index}
	<#else>
		这是奇数-->${item_index}
	</#if>
</#list>
           

日期类型格式化

//?date ?time  ?datetime ?string 必须指定 任选其一 
当前日期${date?date}</br>
当前时间${date?time}</br>
当前完整日期${date?datetime}</br>
当前格式化日期${date?string("yyyy//MM//dd HH:mm:ss")}</br>
           

Null值的处理

对于这个数据在模板引用形式为

${test!"空空空"}   //如果是null 必须指定! //如果是null--->输出空空空
${test!""}  //输出空
${test!} 	//输出空
           

判断NULL

<#if test??>
	1没空没空
<#else>
	2空了空了
</#if>
//test 不为空  执行1
//test 为空    执行2
           

Include标签

<#include "template.htm"/> //取相对路径,包含页面
           

所有代码显示效果

freemark什么是freemarkJavaApi 用法freemark模板的语法所有代码显示效果

继续阅读