FreeMarker five: FreeMarker basic syntax: iterate List list; iterate Map; (Note: LinkedHashMap can ensure that the Map is in order)

When the data prepared in the background is List or Map, how to use Freemarker to get it!

The ftl file is a script! ! !

table of Contents

One: Use the list tag to iterate the list:

Two: Use the list tag to iterate the map:


One: Use the list tag to iterate the list:

Preparation: FreeMarkerSample2: Prepare List data:

package com.imooc.freemarker;

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

import com.imooc.freemarker.entity.Computer;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

public class FreeMarkerSample2 {

	public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		// TODO Auto-generated method stub
		// 1.加载模板
		// Configuration对象是FreeMarker的核心配置对象
		// 1.1创建核心配置对象
		Configuration config = new Configuration(Configuration.VERSION_2_3_31);// 因为我们下载的freemarker版本为2.3.31;所以这儿构造参数为2_3_31;
		// 1.2设置加载的目录;    这儿的意思:在FreeMarkerSample2这个类所在包中去加载指定的ftl文件;第二个参数传递一个空字符串代表是当前包;
		config.setClassForTemplateLoading(FreeMarkerSample2.class, "");
		// 1.3得到模板对象;;;; 以为1.2中设置了,只去扫描FreeMarkerSample1这个类所在包中的ftl,所以1.3这一步getTemplate()参数只需要指定ftl文件名字就可以了;
		Template t = config.getTemplate("sample2.ftl");
		// 2.创建数据 ;;;;;对于Freemaker来说,其数据就是一个Map类型的对象;
		// Map用来包含需要向模板中传入的数据
		Map<String,Object> dataMap = new HashMap<String,Object>();
		List<Computer> computers = new ArrayList<Computer>();
		computers.add(new Computer("123","ThinkPad1",2,null,new Date(),1000f,new HashMap()));
		computers.add(new Computer("456","ThinkPad2",1,"李四",new Date(),2000f,new HashMap()));
		computers.add(new Computer("789","ThinkPad3",1,"王五",new Date(),2000f,new HashMap()));
		dataMap.put("computersList", computers);
		// 3.产生输出;;;这个输出,可以向文件输入,可以向控制台输出,可以向网络流中输出;向什么地方输出,取决于使用哪种类型的输出对象;
		t.process(dataMap, new OutputStreamWriter(System.out)); 
		// System.out:是向控制台输出的核心对象;System.out这个对象本质是一个PrintStream类型的对象,而process()方法的第二个参数需要是Writer类型的对象,所以使用new OutputStreamWriter(System.out),转换一下;
		
	}

}

sample2.ftl: It can be found that this iterative loop is easy to understand; cl_index saves the index of the loop, starting from 0

<#list computersList as cl>
序号,循环的索引:${cl_index}
sn:${cl.sn}
型号:${cl.model}
<#switch cl.state>
<#case 1>
状态:正在使用
<#break>
<#case 2>
状态:闲置
<#break>
<#case 3>
状态:已经作废
<#break>
</#switch>
<#if cl.user??>
用户:${cl.user}
</#if>
采购日期:${cl.dop?string("yyyy年MM月dd日 HH:mm:ss SSS")}
采购价格:${cl.price}
**********
</#list>

effect:


Two: Use the list tag to iterate the map:

To demonstrate Map: In FreeMarkerSample2, add a Map collection to dataMap, computer_map;

When iterating the map, first obtain the key set, and then obtain the corresponding value in the map according to the key;

Note: LinkedHashMap can ensure that the Map is in order ;

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/114637482