淘淘商城49-freemaker页面静态化的模板语法

版权声明:本文为博主原创文章,如有转载请注明出处,谢谢。 https://blog.csdn.net/pdsu161530247/article/details/82148520

目录

1.访问pojo中的属性

1.1创建一个pojo

1.2创建一个模板文件

1.3提供model生成静态页面

2.取list集合中的值并设置下标

2.1在模板中添加取值

2.2提供model

2.3生成静态页面并访问

3.取map中的值的两种方法

3.1在模板中添加取值

3.2提供model

3.3生成静态页面并访问

4.ifelse判断

4.1在模板中添加判断

4.2生成静态页面并访问

5.日期格式化

5.1在模板中添加取值

5.2提供model

5.3生成静态页面并访问

6.null值处理的两种方式

6.1在模板中添加处理null的逻辑

6.2提供model

6.3生成静态页面并访问

7.include标签

7.1在模板中添加include

7.2提供model

7.3生成静态页面并访问

8.所有代码


1.访问pojo中的属性

1.1创建一个pojo

package com.freemarker.test;

public class Person {
	private long id;
	private String name;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Person(long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
}

1.2创建一个模板文件

其中freemarker为生成静态页面的位置,template为模板文件的位置

?c是格式化数字用的

<html>
<head>
	<title>freemarker的api使用</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h3>一、访问pojo的数据</h3>
	个人信息:<br>
	身份证号:${person.id}<br>
	身份证号2:${person.id?c}<br>
	姓名:${person.name}<br>
<hr>
</body>
</html>

1.3提供model生成静态页面

@Test
	public void genHtmlTest1() throws IOException, TemplateException {
		// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
		Configuration configuration = new Configuration(Configuration.getVersion());
		// 第二步:设置模板文件所在的路径。
		configuration.setDirectoryForTemplateLoading(new File(
				"F:\\code\\eclisep-taotao\\freemarker\\src\\main\\resources\\template"));
		// 第三步:设置模板文件使用的字符集。一般就是utf-8.
		configuration.setDefaultEncoding("utf-8");
		// 第四步:加载一个模板,创建一个模板对象。
		Template template = configuration.getTemplate("template.ftl");
		// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
		/**访问pojo的数据*/
		Map<Object, Object> model = new HashMap<>();
		model.put("person", new Person(1001001l,"张三"));
		
		// 第六步:创建一个Writer对象,指定生成的文件名。
		FileWriter writer = new FileWriter(new File("F:\\code\\eclisep-taotao\\freemarker\\src\\main\\resources\\freemarker\\template.html"));
		// 第七步:调用模板对象的process方法输出文件。
		template.process(model, writer);
		// 第八步:关闭流。
		writer.close();
	}

运行后,会发现freemarker目录下生成一个template.html静态页面

访问测试:

2.取list集合中的值并设置下标

2.1在模板中添加取值

<#list personList as person>可以遍历list,${person_index}取下标

<h3>二、循环取list值,并设置下标</h3>
	用户列表:
	<br>
	<table border="1">
		<tr>
			<th>下标</th>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<#list personList as person>
		<tr>
			<td>${person_index}</td>
			<td>${person.id?c}</td>
			<td>${person.name}</td>
		</tr>
		</#list>
	</table>
<hr>

2.2提供model

/**循环取list集合中的数据,并设置下标*/
		List<Person> list = new ArrayList<>();
		list.add(new Person(1001l,"张三"));
		list.add(new Person(1002l,"李四"));
		list.add(new Person(1003l,"王二麻子"));
		model.put("personList", list);

2.3生成静态页面并访问

3.取map中的值的两种方法

3.1在模板中添加取值

<#list personMap?keys as key>取出所有key。${personMap[key].name}在map中根据key取value

<h3>三、取map值的两种方法</h3>
	第一种方法:
	<br>
	<table border="1">
		<tr>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<#list personMap?keys as key>
		<tr>
			<td>${personMap[key].id?c}</td>
			<td>${personMap[key].name}</td>
		</tr>
		</#list>
	</table><br>
	第二种方法:
	<br>
	<table border="1">
		<tr>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<tr>
			<td>${personMap.p1.id?c}</td>
			<td>${personMap.p1.name}</td>
		</tr>
		<tr>
			<td>${personMap.p2.id?c}</td>
			<td>${personMap.p2.name}</td>
		</tr>
		<tr>
			<td>${personMap.p3.id?c}</td>
			<td>${personMap.p3.name}</td>
		</tr>
	</table>
<hr>

3.2提供model

/**取map集合*/
		Map<Object, Object> map = new HashMap<>();
		map.put("p2",new Person(1001l,"张三"));
		map.put("p3",new Person(1002l,"李四"));
		map.put("p1",new Person(1003l,"王二麻子"));
		model.put("personMap", map);

3.3生成静态页面并访问

运行model,即可生成静态页面

4.ifelse判断

4.1在模板中添加判断

判断的格式:

<#if xxx>

<#else>

</#if>

<h3>四、ifelse判断</h3>
用户列表:
	<br>
	<table border="1">
		<tr>
			<th>下标</th>
			<th>根据下标判断奇偶数</th>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<#list personList as person>
		<tr>
			<td>${person_index}</td>
			<#if person_index%2 ==0>
				<td>这里的下标是偶数!!</td>
			<#else>
				<td>这里的下标是奇数!!</td>
			</#if>
			<td>${person.id?c}</td>
			<td>${person.name}</td>
		</tr>
		</#list>
	</table>
<hr>

4.2生成静态页面并访问

5.日期格式化

5.1在模板中添加取值

<h3>五、日期格式化</h3>
	当前日期:${date?date}<br>
	当前时间:${date?time}<br>
	当前日期和时间:${date?datetime}<br>
	自定义日期格式:${date?string("yyyyMM/dd HH:mm:ss")}<br>
<hr>

5.2提供model

/**日期类型格式化*/
		model.put("date", new Date());

5.3生成静态页面并访问

6.null值处理的两种方式

6.1在模板中添加处理null的逻辑

<h3>六、null值处理的两种方式</h3>
第一种:
	${None!"这个值为null,在这里可以设置默认值!"}<br>
第二种:
	<#if None??>
		这个值不为null
	<#else>
		这个值为null,在这里可以设置默认值!
	</#if>
<hr>

6.2提供model

提供一个null,供模板判断

/**null值处理*/
		model.put("None", null);

6.3生成静态页面并访问

输出两种都为null

访问页面

7.include标签

7.1在模板中添加include

<h3>七、include标签</h3>
	添加的页脚:<#include "hello.ftl" />
<hr>

 hello.ftl中的内容

${hello }

7.2提供model

为include的提供model

/**添加页脚*/
model.put("hello", "已添加页脚helle.ftl");

7.3生成静态页面并访问

8.所有代码

GenHtm.java的代码

package com.freemarker.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

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

public class GenHtm {
	// 生成静态页面的方法
	@Test
	public void genHtmlTest1() throws IOException, TemplateException {
		// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
		Configuration configuration = new Configuration(Configuration.getVersion());
		// 第二步:设置模板文件所在的路径。
		configuration.setDirectoryForTemplateLoading(new File(
				"F:\\code\\eclisep-taotao\\freemarker\\src\\main\\resources\\template"));
		// 第三步:设置模板文件使用的字符集。一般就是utf-8.
		configuration.setDefaultEncoding("utf-8");
		// 第四步:加载一个模板,创建一个模板对象。
		Template template = configuration.getTemplate("template.ftl");
		// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
		/**访问pojo的数据*/
		Map<Object, Object> model = new HashMap<>();
		model.put("person", new Person(1001001l,"张三"));
		/**循环取list集合中的数据,并设置下标*/
		List<Person> list = new ArrayList<>();
		list.add(new Person(1001l,"张三"));
		list.add(new Person(1002l,"李四"));
		list.add(new Person(1003l,"王二麻子"));
		model.put("personList", list);
		/**取map集合*/
		Map<Object, Object> map = new HashMap<>();
		map.put("p2",new Person(1001l,"张三"));
		map.put("p3",new Person(1002l,"李四"));
		map.put("p1",new Person(1003l,"王二麻子"));
		model.put("personMap", map);
		/**日期类型格式化*/
		model.put("date", new Date());
		/**null值处理*/
		model.put("None", null);
		/**添加页脚*/
		model.put("hello", "已添加页脚helle.ftl");
		// 第六步:创建一个Writer对象,指定生成的文件名。
		FileWriter writer = new FileWriter(new File("F:\\code\\eclisep-taotao\\freemarker\\src\\main\\resources\\freemarker\\template.html"));
		// 第七步:调用模板对象的process方法输出文件。
		template.process(model, writer);
		// 第八步:关闭流。
		writer.close();
	}
}

template.ftl的代码:

<html>
<head>
	<title>freemarker的api使用</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h3>一、访问pojo的数据</h3>
	个人信息:<br>
	身份证号:${person.id}<br>
	身份证号2:${person.id?c}<br>
	姓名:${person.name}<br>
<hr>
<h3>二、循环取list值,并设置下标</h3>
	用户列表:
	<br>
	<table border="1">
		<tr>
			<th>下标</th>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<#list personList as person>
		<tr>
			<td>${person_index}</td>
			<td>${person.id?c}</td>
			<td>${person.name}</td>
		</tr>
		</#list>
	</table>
<hr>
<h3>三、取map值的两种方法</h3>
	第一种方法:
	<br>
	<table border="1">
		<tr>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<#list personMap?keys as key>
		<tr>
			<td>${personMap[key].id?c}</td>
			<td>${personMap[key].name}</td>
		</tr>
		</#list>
	</table><br>
	第二种方法:
	<br>
	<table border="1">
		<tr>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<tr>
			<td>${personMap.p1.id?c}</td>
			<td>${personMap.p1.name}</td>
		</tr>
		<tr>
			<td>${personMap.p2.id?c}</td>
			<td>${personMap.p2.name}</td>
		</tr>
		<tr>
			<td>${personMap.p3.id?c}</td>
			<td>${personMap.p3.name}</td>
		</tr>
	</table>
<hr>
<h3>四、ifelse判断</h3>
用户列表:
	<br>
	<table border="1">
		<tr>
			<th>下标</th>
			<th>根据下标判断奇偶数</th>
			<th>身份证号</th>
			<th>姓名</th>
		</tr>
		<#list personList as person>
		<tr>
			<td>${person_index}</td>
			<#if person_index%2 ==0>
				<td>这里的下标是偶数!!</td>
			<#else>
				<td>这里的下标是奇数!!</td>
			</#if>
			<td>${person.id?c}</td>
			<td>${person.name}</td>
		</tr>
		</#list>
	</table>
<hr>
<h3>五、日期格式化</h3>
	当前日期:${date?date}<br>
	当前时间:${date?time}<br>
	当前日期和时间:${date?datetime}<br>
	自定义日期格式:${date?string("yyyyMM/dd HH:mm:ss")}<br>
<hr>
<h3>六、null值处理的两种方式</h3>
第一种:
	${None!"这个值为null,在这里可以设置默认值!"}<br>
第二种:
	<#if None??>
		这个值不为null
	<#else>
		这个值为null,在这里可以设置默认值!
	</#if>
<hr>
<h3>七、include标签</h3>
	添加的页脚:<#include "hello.ftl" />
<hr>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/pdsu161530247/article/details/82148520