Wednesday, 17 June 2020

FreeMarker: #list directive

#list directive is used to list the collection of elements.

 

Syntax

<#list sequence as item>

    Part repeated for each item

</#list>

 

employees.ftl

Id   		first_name  	last_name
-------------------------------------------
<#list employees as employee>
${employee.id} 	 	${employee.firstName}		${employee.lastName}
</#list>

Step 1: Define Employee class.

 

Employee.java

package com.sample.app.templates.model;

public class Employee {
	private int id;
	private String firstName;
	private String lastName;

	public Employee(int id, String firstName, String lastName) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}

Step 2: Define ‘FreeMarkerUtil’ class that take model class and template file as input and merge them.

 

FreeMarkerUtil.java

package com.sample.app.util;

import java.io.StringWriter;
import java.util.Locale;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreeMarkerUtil {

	private static final Configuration FREE_MARKER_CONFIGURATION = new Configuration(Configuration.VERSION_2_3_30);

	static {
		FREE_MARKER_CONFIGURATION.setClassForTemplateLoading(FreeMarkerUtil.class, "/templates/");
		FREE_MARKER_CONFIGURATION.setDefaultEncoding("UTF-8");
		FREE_MARKER_CONFIGURATION.setLocale(Locale.US);
		FREE_MARKER_CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
	}

	public static StringWriter mergeModelAndTemplate(Object modelObject, String ftlFile) throws Exception {
		StringWriter stringWriter = new StringWriter();

		Template template = FREE_MARKER_CONFIGURATION.getTemplate(ftlFile);

		template.process(modelObject, stringWriter);

		return stringWriter;
	}

}

Step 3: Define EmployeePopulator.

 

EmployeePopulator.java

package com.sample.app;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sample.app.templates.model.Employee;
import com.sample.app.util.FreeMarkerUtil;

public class EmployeesPopulator {
	public static void main(String args[]) throws Exception {
		Employee emp1 = new Employee(1, "Krishna", "Gurram");
		Employee emp2 = new Employee(2, "Ram", "Kota");
		Employee emp3 = new Employee(3, "Gopi", "Battu");
		Employee emp4 = new Employee(4, "Joel", "Chelli");

		List<Employee> empList = Arrays.asList(emp1, emp2, emp3, emp4);

		Map<String, Object> modelObject = new HashMap<String, Object>();
		modelObject.put("employees", empList);

		StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "employees.ftl");
		System.out.println(stringWriter.toString().trim());

	}
}

Output

Id   		first_name  	last_name
-------------------------------------------
1 	 	Krishna		Gurram
2 	 	Ram		Kota
3 	 	Gopi		Battu
4 	 	Joel		Chelli


Previous                                                    Next                                                    Home

No comments:

Post a Comment