Freemarker template minimal case implementation

what is freemarker

Baidu Encyclopedia:

FreeMarker is a template engine : a universal tool for generating output text ( HTML web pages, emails , configuration files , source code, etc.) based on templates and data to be changed . It is not for end users, but a Java class library, a component that programmers can embed into the products they develop.

FreeMarker is free and released under the Apache License version 2.0. Its template is written in FreeMarker Template Language (FTL), which is a simple and dedicated language. Data needs to be prepared for display in real programming languages, such as database queries and business operations, and then the template displays the prepared data. In the template, it is mainly used for how to display data, while outside the template, attention is paid to what data to display [1] 。

rely

Use spring boot to develop and add the following dependencies:

<dependencies>
        <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>
</dependencies>

controller:

package com.monkey.freemarker.controller;

import com.monkey.freemarker.entity.Item;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.RequestWrapper;
import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author :XXX
 * @date :Created in 2021/11/18 0018 8:34
 * @description:
 * @modified By:
 * @version: v1.0
 */
@RestController
public class ItemController {

    @Autowired
    private Configuration configuration;

    @RequestMapping("create")
    public String createHtml() throws Exception {
        // 获取模板
        Template template = configuration.getTemplate("item.tpl");

        // 数据填充模板
        Map<String, Object> dataModel = new HashMap<>();
        dataModel.put("item", new Item("iphone 13", new BigDecimal(10000), 10000));
        String path = "D:/tplHtml";
        File file = new File(path + "/item-" + 100 + ".html");
        if (file.exists()) {
            System.out.println("文件已存在");
        } else {
            file.createNewFile();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            template.process(dataModel, bufferedWriter);
        }
        return "create success";
    }
}

entity: 

package com.monkey.freemarker.entity;

import java.math.BigDecimal;

/**
 * @author :XXX
 * @date :Created in 2021/11/18 0018 8:34
 * @description:商品
 * @modified By:
 * @version: v1.0
 */
public class Item {

    private String goodName;

    private BigDecimal goodPrice;

    private int evaluateCount;

    public Item(String goodName, BigDecimal goodPrice, int evaluateCount){
        this.evaluateCount = evaluateCount;
        this.goodName = goodName;
        this.goodPrice = goodPrice;
    }

    public String getGoodName() {
        return goodName;
    }

    public void setGoodName(String goodName) {
        this.goodName = goodName;
    }

    public BigDecimal getGoodPrice() {
        return goodPrice;
    }

    public void setGoodPrice(BigDecimal goodPrice) {
        this.goodPrice = goodPrice;
    }

    public int getEvaluateCount() {
        return evaluateCount;
    }

    public void setEvaluateCount(int evaluateCount) {
        this.evaluateCount = evaluateCount;
    }
}

tpl:

<h1>商品详情页</h1>
商品展示
 <p>商品名称:${item.goodName }</p>
 <p>商品价格:${item.goodPrice }</p>
 <p>商品评价数:${item.evaluateCount }</p>

Project directory structure:

 Rendering:

 

Guess you like

Origin blog.csdn.net/u010960161/article/details/121410243