Thursday 2 July 2020

FreeMarker: Access map or hash content using index notation

In my previous post, I explained how to access nested properties using ’.’ notation, you can even use [] notation to access the value associated with the key.

 

For example, below map define employee details.

 

<#assign empDetails={ "id" : 1, "name": "krishna", "address" : { "city": "Bangalore", "country" : "India"} } >

 

Now you can access id and name property like below.

id : ${empDetails["id"]}

name : ${empDetails["name"]}

 

You can access nested property city in either of below notation.

city : ${empDetails["address"]["city"]}

city : ${empDetails["address"].city}

city : ${empDetails.address["city"]}

 

Find the below working application.

 

Step 1: Define ‘mapIndexNotation.ftl’ file under src/main/resources/templates folder.

 

mapIndexNotation.ftl

<#assign empDetails={ "id" : 1, "name": "krishna", "address" : { "city": "Bangalore", "country" : "India"} } >

id : ${empDetails["id"]}
name : ${empDetails["id"]}

city : ${empDetails["address"]["city"]}
city : ${empDetails["address"].city}
city : ${empDetails.address["city"]}

country : ${empDetails["address"]["country"]}
country : ${empDetails["address"].country}
country : ${empDetails.address["country"]}

Step 2: Define ‘FreeMarkerUtil’ class that takes a model object 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);
		FREE_MARKER_CONFIGURATION.setFallbackOnNullLoopVariable(false);
	}

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

 

MapIndexNotationPopulator.java

package com.sample.app;

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

import com.sample.app.util.FreeMarkerUtil;

public class MapIndexNotationPopulator {
	public static void main(String args[]) throws Exception {

		Map<String, Object> modelObject = new HashMap<String, Object>();
		
		StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "mapIndexNotation.ftl");
		System.out.println(stringWriter.toString().trim());

	}
}

Output

id : 1
name : 1

city : Bangalore
city : Bangalore
city : Bangalore

country : India
country : India
country : India



Previous                                                    Next                                                    Home

No comments:

Post a Comment