Introduction and use of freemarker

Keywords: FreeMarker Java xml JSP

Introduction and use of freemarker

1, What is freemarker

FreeMarker is a template engine written in Java language, which generates text output based on templates. FreeMarker has nothing to do with the Web container, that is, it doesn't know about servlets or HTTP when the Web is running. It can not only be used as the implementation technology of presentation layer, but also be used to generate XML, JSP or Java.

In current enterprises, Freemarker is mainly used for static page or page display

2, How to use Freemarker

Add freemarker's jar package to the project
Maven project add dependency

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>

Principle:
Use steps:
Step 1: create a Configuration object and directly new an object. The parameter of the constructor is the version number of freemarker.

// Step 1: create a Configuration object and directly new an object. The parameter of the constructor is the version number of freemarker.
Configuration configuration = new Configuration(Configuration.getVersion());

Step 2: set the path of the template file.
All subsequent template files are placed in this directory

// Step 2: set the path of the template file.
		configuration.setDirectoryForTemplateLoading(
				new File("F:/dianshang/e3-item-web/src/main/webapp/WEB-INF/ftl"));

Step 3: set the character set used by the template file. It's usually utf-8

// Step 3: set the character set used by the template file. It's usually utf-8
		configuration.setDefaultEncoding("utf-8");

Step 4: load a template and create a template object.

// Step 4: load a template and create a template object.
		Template template = configuration.getTemplate("hello.ftl");

Step 5: create a dataset used by the template, which can be pojo or map. Generally map.

// Step 5: create a dataset used by the template, which can be pojo or map. Generally map.
		Map dataModel = new HashMap<>();
		// Add data to dataset
		dataModel.put("hello", "this is my first freemarker test.");

Step 6: create a Writer object, usually a FileWriter object, and specify the generated file name.
Create a folder on the local disk to save the generated files

// Step 6: create a Writer object, usually a FileWriter object, and specify the generated file name.
		Writer out = new FileWriter(new File("F:/test/freemarker/hello.txt"));

Step 7: call the process method of the template object to output the file.

// Step 7: call the process method of the template object to output the file.
		template.process(dataModel, out);

Step 8: close the flow.

// Step 8: close the flow.
		out.close();

View results

3, The syntax of freemarker template

3.1 access key in map

${key}
Case study

3.2 accessing properties in pojo

Create pojo entity

public class Student {

	private Integer id;
	private String name;
	private Integer age;
	private String address;
	
	. . .  get set Method
	
	public Student(Integer id, String name, Integer age, String address) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public Student(){
		
	}
}

Create template object
Code:

	@Test
	public void testFreeMarkerPojo() throws Exception {
		// Step 1: create a Configuration object and directly new an object. The parameter of the constructor is the version number of freemarker.
		Configuration configuration = new Configuration(Configuration.getVersion());
		// Step 2: set the path of the template file.
		configuration.setDirectoryForTemplateLoading(
				new File("F:/dianshang/e3-item-web/src/main/webapp/WEB-INF/ftl"));
		// Step 3: set the character set used by the template file. It's usually utf-8
		configuration.setDefaultEncoding("utf-8");
		// Step 4: load a template and create a template object.
		Template template = configuration.getTemplate("student.ftl");
		// Step 5: create a dataset used by the template, which can be pojo or map. Generally map.
		Map dataModel = new HashMap<>();
		// Add data to dataset
		//Create a pojo object
		Student student = new Student(1,"Xiao Ming",18,"Beijing");
		dataModel.put("student", student);
		
		// Step 6: create a Writer object, usually a FileWriter object, and specify the generated file name.
		Writer out = new FileWriter(new File("F:/test/freemarker/student.html"));
		// Step 7: call the process method of the template object to output the file.
		template.process(dataModel, out);
		// Step 8: close the flow.
		out.close();
	}

Result:

3.3 fetching the data in the set

<#list studentList as student>
${student.id}/${studnet.name}
</#list>

3.4 date type format

3.5 processing of null value

3.6 import other template Include labels

55 original articles published, 3 praised, 20000 visitors+
Private letter follow

Posted by corsc on Mon, 16 Mar 2020 00:29:56 -0700