Wednesday 24 June 2020

FreeMarker: items directive

‘items’ directive is used if you want to print something before the first list item and something after the last list item.

 

Syntax

<#list listOfItems>

    <#items as item>
      <li>${item}</li>
    </#items>

</#list>

employeesInfo.ftl

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

	Done printing all the employees information
</#list>

Step 1: Define Employee model 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 EmployeesInfoPopulator.

 

EmployeesInfoPopulator.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 EmployeesInfoPopulator {
	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, "employeesInfo.ftl");
		System.out.println(stringWriter.toString());

	}
}

Output

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

	Done printing all the employees information

A list directive with items also can have an else directive:

Syntax

<#list listOfItems>

    <#items as item>
      <li>${item}</li>
    </#items>

<#else>
	.....
</#list>

When listOfItems is empty, then the snippet after #else directive gets executed.

 

employeesInfo1.ftl

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

	Done printing all the employees information

<#else>
No Employees exist
</#list>

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 EmployeesInfoPopulator1.

 

EmployeesInfoPopulator1.java

package com.sample.app;

import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.sample.app.util.FreeMarkerUtil;

public class EmployeesInfoPopulator1 {
	public static void main(String args[]) throws Exception {
		Map<String, Object> modelObject = new HashMap<String, Object>();
		modelObject.put("employees", Collections.EMPTY_LIST);

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

	}
}

Output

No Employees exist


Previous                                                    Next                                                    Home

No comments:

Post a Comment