Thursday, 11 June 2020

FreeMarker: Nested Objects example

Using ‘.’ Operator, we can access the nested objects.

 

For example, a User instance has address details nested in it. We can map the user address in template below.

 

street: ${user.address.street}

city: ${user.address.city}

country: ${user.address.country}

 

Find the below working application.

 

Step 1: Create userDetails.ftl file under src/main/resources folder.

 

userDetails.ftl
name: ${user.name}
age: ${user.age}
street: ${user.address.street}
city: ${user.address.city}
country: ${user.address.country}

Step 2: Define Address model class.

 

Address.java

package com.sample.app.templates.model;

public class Address {
	private String street;
	private String city;
	private String country;

	public Address(String street, String city, String country) {
		super();
		this.street = street;
		this.city = city;
		this.country = country;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

}

Step 3: Define UserDetails class.

 

UserDetails.java
package com.sample.app.templates.model;

public class UserDetails {

	private String name;
	private int age;
	private Address address;

	public UserDetails(String name, int age, Address address) {
		super();
		this.name = name;
		this.age = age;

		this.address = address;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

}

Step 4: 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 5: Define ‘UserDetailsPopulator’ class.

 

UserDetailsPopulator.java

package com.sample.app;

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

import com.sample.app.templates.model.Address;
import com.sample.app.templates.model.UserDetails;
import com.sample.app.util.FreeMarkerUtil;

public class UserDetailsPopulator {
	public static void main(String args[]) throws Exception {
		Map<String, Object> modelObject = new HashMap<String, Object>();

		Address address = new Address("Chowdeswarit Steet", "Bangalore", "India");

		modelObject.put("user", new UserDetails("Krishna", 31, address));

		StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "userDetails.ftl");

		System.out.println(stringWriter);
	}
}

Output

name: Krishna

age: 31

street: Chowdeswarit Steet

city: Bangalore

country: India



Previous                                                    Next                                                    Home

No comments:

Post a Comment