What is a template engine?
1. The browser requests the web server
2. The server renders the page. The rendering process is to fill the jsp page (template) with data (model).
3. The server returns the rendered page to the browser.
So the template engine is: template + data = output, the Jsp page is the template, the jsp tag embedded in the page is the data, and the combination of the two outputs the html web page.
Freemarker Quick Start
Add the integration package of Freemarker and SpringBoot
XML <!-- Spring Boot 对结果视图 Freemarker 集成 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> |
Configure freemarker for the content management interface layer in nacos, and add a new freemarker-config-dev.yaml to the public configuration group
The configuration information is as follows:
spring: freemarker: enabled: true cache: false #Turn off template cache for easy testing settings: template_update_delay: 0 suffix: .ftl #page template suffix name charset: UTF-8 template-loader-path: classpath:/templates/ #page template Location (default is classpath:/templates/) resources: add-mappings: false #Close static resource mapping in the project (resources under static, resources folder) |
Add freemarker-config-dev.yaml to the configuration file
Add a template, create the templates directory under resources, and add the test.ftl template file
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello World!</title> </head> <body> Hello ${name}! </body> </html> |
Write controller method and prepare model data
Java package com.xuecheng.content.api;
import org.bouncycastle.math.raw.Mod; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web. servlet.ModelAndView;
import java.util.Map;
@Controller public class FreemarkerController { @GetMapping("/testfreemarker") public ModelAndView test(){ ModelAndView modelAndView = new ModelAndView(); //Set model data modelAndView.addObject("name ","Xiao Ming"); //Set the template name modelAndView.setViewName("test"); return modelAndView; } }
|
浏览器访问该接口就可以看到该门户了