SpringBoot2.1.5 (32) --- SpringBoot integration Freemaker template engine

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/zhangbijun1230/article/details/91357378


A, Freemarker
1, Maven dependence:
2, YML file configuration:
3, create a template file Freemarker
4, create a test the Controller
5, start the project, to test
a, Freemarker
Description:
FreeMarker is a template engine: that is, a template-based and to change the data and to generate output text (HTML pages, e-mail, the configuration file, source code, etc.) the common tools. It is not for end users, but a Java class library that programmers can embed a component of their product development.

Writing templates for FreeMarker Template Language (FTL). It is simple, specific language, like PHP is not as mature programming language. That means to prepare data to be displayed on a real programming language, such as database queries and business operations, after the template has been prepared to display data. In the template, you can focus on how to present the data, but outside of a template can focus on what you want to display data.

Official website:
https://freemarker.apache.org/

Online manual:
template common syntax, you can refer to the Chinese online manual, not repeat them here
http://freemarker.foofun.cn/ Second, the actual code is:
1, rely on Maven:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



2, yml configuration file:

spring:
  application:
    name: freemarker
  freemarker:
    # 禁用模板缓存
    cache: false
    # 编码格式
    charset: UTF-8
    # freemarker模板后缀 默认是 .ftl
    suffix: .html
    # 是否为此技术启用MVC视图分辨率。
    enabled: true
    # Content-Type值
    content-type: text/html
    # #模板加载路径 按需配置 ,默认路径是 classpath:/templates/
    template-loader-path: classpath:/templates/



3. Create a template file Freemarker
Thanks to a template suffix in the configuration as .html (default is .ftl), so here create an index.html file
document reads as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
Hello ${content}
</body>
</html>

 

å¨è¿éæå ¥ å¾çæè¿ °

Guess you like

Origin blog.csdn.net/zhangbijun1230/article/details/91357378