当前位置:网站首页>Freemaker template engine
Freemaker template engine
2022-07-25 08:30:00 【Two kings and one today】
freemarker Introduce
FreeMarker It's a template engine : It's based on the template and the data to be changed , And used to generate output text (HTML Webpage , E-mail , The configuration file , Source code, etc ) General tools for . It's not for end users , It is a Java Class library , It's a component that programmers can embed in their products .
The template is written as FreeMarker Template Language (FTL). It's simple , Special language , No image PHP That mature programming language . That means preparing data for display in real programming languages , For example, database query and business operation , After that, the template shows the prepared data . In the template , You can focus on how to present data , Outside of the template, you can focus on what data to show .

frequently-used java What other template engines are there ?
Jsp、Freemarker、Thymeleaf 、Velocity etc. .
Jsp by Servlet special , Cannot be used alone .
Thymeleaf For new technology , More powerful , But the efficiency of implementation is relatively low .
Velocity from 2010 Updated in 2.0 After version , There is no update .Spring Boot The official in the 1.4 This is no longer supported after version , although Velocity stay 2017 The year version is iterated , But it's too late .
Page rendering process

Use dependency :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- apache Yes java io Package tool library -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>The configuration file
server:
port: 8881 # Service port
spring:
application:
name: freemarker-demo # Specify the service name
freemarker:
cache: false # Turn off template caching , Convenient test
settings:
template_update_delay: 0 # Check the template update delay time , Set to 0 Means to check immediately , If the time is longer than 0 There will be caches that are not convenient for template testing
suffix: .ftl # Appoint Freemarker The suffix of the template file Create a template
stay resources Create templates, This directory is freemarker The default template storage directory .
stay templates Create a template file under 01-basic.ftl , The interpolation expression in the template will eventually be freemarker Replace with specific data .
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World!</title>
</head>
<body>
<b> Plain text String Exhibition :</b><br><br>
Hello ${name} <br>
<hr>
<b> object Student Data presentation in :</b><br/>
full name :${stu.name}<br/>
Age :${stu.age}
<hr>
<ul>
<#-- Sentenced to empty 、 Conditions 、 A collection of traverse -->
</ul>
<#-- Arithmetic operations 、 Compare 、 Logical operations -->
<#-- Built in functions -->
</body>
</html>establish controller
establish Controller class , towards Map Add name, Finally, return to the template file .
package com.heima.demo.freemarker.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Controller
public class HelloController {
@GetMapping("/basic")
public String test(Model model) {
//1. Parameters in plain text
model.addAttribute("name", "freemarker");
//2. Parameters related to entity classes
Map<String, Object> student = new HashMap<>();
student.put("name"," Xiao Ming ");
student.put("age", 18);
model.addAttribute("stu", student);
return "01-basic";
}
}Create startup class
package com.heima.demo.freemarker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FreemarkerDemotApplication {
public static void main(String[] args) {
SpringApplication.run(FreemarkerDemotApplication.class,args);
}
}test
request :http://localhost:8881/basic
边栏推荐
- Redis fragment cluster
- "Unable to recognize" yarn "item as cmdlet, function, script file
- Svg creative underline style JS special effect
- Learn no when playing 8 | the enterprise saves hundreds of thousands in an instant, just because it uses it
- Idea failed to start the project yamlexception Chinese file encoding format
- A powerful port scanning tool (nmap)
- Install MySQL 8.0 using docker
- Machine learning theory and case analysis (Part1) -- Fundamentals of machine learning
- [audio and video] picture YUV data format
- 一次简单的SQL注入靶场练习
猜你喜欢

Implementation of depth first and breadth first traversal of binary tree

Chapter 3 business function development (query clues)

Message Oriented Middleware

一次简单的SQL注入靶场练习

CentOS 8.2 MySQL installation (xshell6)

Apartment repair reporting system (idea, SSM, MySQL)

Rk3399 development board i2c4 attaching EEPROM instance

【黑马程序员】Redis学习笔记001:Redis简介+五种基本数据类型

NVIDIA programmable reasoning accelerator tensorrt learning notes (II) - practical operation

Some easy-to-use plug-ins and settings installed in vscode
随机推荐
2022-07-19 Daily: too many icml2022 papers to read? "One sentence comments on 1234 paper highlights" helps you quickly lock
Brush the title "sword finger offer" day02
Node+js build time server
Eureka forced offline service
Raspberry connects EC20 for PPP dialing
C#入门系列(三十) -- 异常处理
Raspberry pie 4B parsing PWM
[dark horse programmer] redis learning notes 003: redis transactions
Sqlserver has opened the CDC table. Why can we only pull the full amount of data and can't recognize the incremental data
Idea failed to start the project yamlexception Chinese file encoding format
Learn when playing No 6 | the magic of document library lies in
【keras bug】Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ ref(
[Sesame Street family] & Bert Bart Roberta
Apartment repair reporting system (idea, SSM, MySQL)
How to obtain the intersection / subtraction / Union of two sets by MySQL
OpenGL es to achieve the effect of "big head, small head" and "head shaking"
Dirty data and memory leakage of ThreadLocal
TCGA simple download tool V16 installation error
Common commands of raspberry pie
Enq: HW - fault analysis caused by content waiting