当前位置:网站首页>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最佳实践
- RTOS系列(13):汇编LDR指令、LDR伪指令、[Rn]寄存器间接引用 详细解析
- Redis core principles
- Blue and white porcelain used by Charles
- Advanced C language (XII) - dynamic memory management
- Memcached data cache database (improve efficiency)
- @Principle of Autowired annotation
- Machine learning theory and case analysis (Part1) -- Fundamentals of machine learning
- JVM specification Oracle official website
- Network packet loss, network delay? This artifact helps you solve all problems
猜你喜欢

记录两次多端排查问题的过程

Message Oriented Middleware

第3章业务功能开发(查询线索)

A simple hotel background management system based on jsp+servlet+mysql

Redis fragment cluster

Redis分片集群

ArcGIS Pro scripting tool (10) -- generate.Stylx style symbols from layers

Refreshing mobile terminal personal center page

【黑马程序员】Redis学习笔记004:主从复制+哨兵模式+集群

Raspberry connects EC20 for PPP dialing
随机推荐
Raspberry pie 4B parsing PWM
A simple hotel background management system based on jsp+servlet+mysql
Rstudio shows that it can't connect to the web page, or it has a new website.
ArcGIS Pro脚本工具(10)——从图层生成.stylx样式符号
@Use of Autowired
Learn when playing No 5 | human high quality examination, right here →
FreeMaker模板引擎
Huawei device remote login (Telnet, SSH) configuration
JS typewriter animation JS special effect plug-in autotyperjs
Raspberry pie creation self start service
475-82(230、43、78、79、213、198、1143)
ip命令使用详解
SSM+JSP+MYSQL实现的宠物领养收养管理系统源码
Redis best practices
阿里云服务的网络解决方案
Redis fragment cluster
Idea2021 failed to start. Could not find main class com/intellij/idea/main
公寓报修系统(IDEA,SSM,MySQL)
[dark horse programmer] redis learning notes 001: introduction to redis + five basic data types
Raspberry connects EC20 for PPP dialing