1. Common Spring Boot 2.x template engine and official recommendation Case
1) JSP (rear rendering, consumption performance)
Java Server Pages dynamic web technology, by the application server in the JSP engine to compile and execute, and then generate the entire page back to the client. The advantage is: can write java code, support for Expression Language (el, jstl), built-in functions.
But the essence is the JSP Servlet, it is very occupied JVM memory. Java Web official recommendation, but Spring Boot is not recommended https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations
2)Freemarker
FreeMarker Template Language (FTL) files are generally stored as xxx.ftl. Strictly dependent on the MVC pattern, does not depend on Servlet container (do not take the JVM memory), support for built-in functions
3) Thymeleaf (主 推)
Lightweight template engine (responsible for the business logic is not recommended, or DOM XML parsing will take up more memory). Can be opened directly in the browser and display the correct page template. Direct is the end of html, you can directly edit.
2.Spring Boot integration template engine freemarker
Step 1: introducing Freemarker dependent correlation maven
<! - dependent template engine incorporated freemarker -> < dependency > < the groupId > org.springframework.boot </ the groupId > < the artifactId > Spring-Boot-Starter-freemarker </ the artifactId > </ dependency >
Step 2: Freemarker Basic Configuration
# Whether to open thymeleaf cache, local is false, produce recommendations to true
spring.freemarker.cache = false
spring.freemarker.charset = UTF-8
spring.freemarker.allow-Request-the override = false
spring.freemarker.check-Template-LOCATION to true =
# type
spring.freemarker.content-type = text / HTML
spring.freemarker.expose-Request-to true Attributes =
spring.freemarker.expose the session-to true-Attributes =
# file extension
spring.freemarker.suffix = .ftl
# path
spring.freemarker.template-loader-path = classpath: / templates /
Step 3: Create a new file .ftl
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1> ${setting.name} </h1> <h1> ${setting.domain} </h1> </body> </html>
Step 4: Simple test code and write access
@Controller @RequestMapping ( "/ FreeMaker" ) public class FreemakerController { @Autowired Private ServerSettings Setting; @GetMapping ( "Hello" ) public String index (a ModelMap a ModelMap) { modelMap.addAttribute ( "Setting" , Setting); return "FM / index "; // do not add a suffix, which has been specified in the configuration file suffix } }
3.Spring Boot integration template engine thymeleaf
Official website address: https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html
Step 1: introducing thymeleaf dependent correlation maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Step 2: thymeleaf Basic Configuration
# Develop close cached, or can not see the live page
spring.thymeleaf.cache = false
spring.thymeleaf.mode HTML5 =
# Prefix
spring.thymeleaf.prefix = the CLASSPATH: / Templates /
# coding
spring.thymeleaf.encoding = UTF- 8
# type
spring.thymeleaf.content-type = text / HTML
# name suffix
spring.thymeleaf.suffix = .html
Step 3: new .html file
<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-8" > < title > Insert title here Wallpaper </ title > </ head > < body > template engine integrated ADMIN Thymeleaf / info.html < h1 > test content, does not increase the expression th </ h1 of > < h1 of th: text = "$ {} setting.name" > test content </ h1 of > </ body > </html>
Step 4: Simple test code and write access
@Controller @ RequestMapping ( "/ tyhmeleaf" ) public class ThymeleafController { @Autowired Private ServerSettings Setting; @GetMapping ( "the Hello" ) public String index () { return "index"; // do not add the suffix has been specified in the configuration file inside suffix } @GetMapping ( "info" ) public String ADMIN (a ModelMap a ModelMap) { modelMap.addAttribute ( "Setting" , Setting); return "ADMIN / info"; // do not add a suffix, which has been specified in the configuration file suffix } }
Note: $ expression can only be written in the label inside th
Getting Started: https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html