FreeMarker Introduction
Apache FreeMarker ™ is a template engine : a Java library for generating data based on the template and change the text output (HTML pages, e-mail, configuration files, source code, etc.). Template is FreeMarker template language (FTL) written, this is a simple proprietary language (like PHP programming language such complete). Typically, a general-purpose programming language (such as Java) to prepare the data (publication database queries, conduct business computing). Then, Apache FreeMarker ready to use templates to display data. In the template, you will focus on how to present the data, and in addition to templates, you will focus on the data to be presented.
SpringBoot comparable official recommended Thymeleaf, because of their relatively familiar FreeMarker, so here I'll explain how to use FreeMarker current front page in SpringBoot in.
- excerpt from the above FreeMarker official website
SpringBoot procedure using FreeMarker
The first step is to introduce in pom.xml spring-boot-starter-freemarker rely on specific code is as follows:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
The second step is to create a test templates directory under the new freemarkDemo.ftl resources as follows:
<h1>${msg}</h1>
The third step is to create an access freemarkDemo.ftl the Controller.
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg", "SpringBoot With Freemark hello world!");
return "test/helloworld";
}
}
test
Enter Access Controller FreeMarker page in the tour is the URL of: HTTP: // localhost: 8080 / SBE / the Hello / the Test test test results are as follows:
summary
SpringBoot FreeMarker using the following steps:
- Add FreeMarker starter dependent
- Create a template page and access templates page to ask Controller
The sample code
Specific code examples, please see my GitHub repository springbootexamples the module project name: the Spring-the Boot-2.x-FreeMarker view next.
GitHub:https://github.com/zhuoqianmingyue/springbootexamples