Author: Jiang Xia
| zhihu: www.zhihu.com/people/1024…
| GitHub:github.com/JiangXia-10…
| CSDN:blog.csdn.net/qq_4115394…
| the nuggets: juejin. Cn/user / 651387…
| public no. : 1024 notes
This article contains 1,289 words and takes 6 minutes to read
1, the introduction
For more information about creating a Springboot project, see the following article, which is not covered here. See the previous article Getting Started with SpringBoot: Building your first Springboot project using IDEA and Eclipse.
Freemarker template
Start by introducing the freemarker dependency into the POM file as follows:
<! <dependency> <groupId>org.springframework. Boot </groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>Copy the code
Create a new Controller folder under com.example.demo and create a new FreemarkerController class as follows:
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class FreemarkerController { @RequestMapping(value="/freemarker",method = RequestMethod.GET) public String freemarker(Model Model) {Model. AddAttribute ("name", "liqiang "); model.addAttribute("age", 22); model.addAttribute("id", 223); return "freemarker"; }}Copy the code
Configure freemarker in the application.yml file, modify the yML file, create the application file by default, and configure yML as follows:
# yML and the property file function is the same configuration Settings, such as database, port number, etc., but the # structure of YML is a tree structure, and the same content is not repeated because the tree display, but if two files exist in the project, the property is loaded first. Note that eclipse yML files are indented with the TAB key, causing an error. spring freemarker: # prefix: classpath:/templates/ suffix: .ftl content-type: text/html template-loader-path: classpath:/templatesCopy the code
Go to the templates folder and create a new freemarker. FTL file as follows:
<! DOCTYPE html> <html lang="en"> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <body> Name: ${name} < br > < br > age: ${age} < br > < br > number: ${id} < br > < br > < / body > < / HTML >Copy the code
Startup in the browser address bar enter, http://localhost:8080/freemarker page results are as follows:
Freemarker page template created!
Thymeleaf template
Add the thymeleaf dependency to the POM file as follows:
<! > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>Copy the code
Create a new ThymeleafController class in the Controller folder with the following code
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ThymeleafController { @RequestMapping("/thymeleaf") public String test(Model model) { Model. AddAttribute ("name", "查 看 "); model.addAttribute("age", 22); model.addAttribute("id", 223); return "thymeleaf"; }}Copy the code
Configure thymeleaf in the application.yml file with the following code:
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
cache: false
content-type: text/html
Copy the code
Then go to the Templates folder and create a new thymeleaf.html page file as follows:
<! DOCTYPE HTML >< HTML >< head> <meta charset=" utF-8 "> <title></title> </head> <body> < input type = "text" th: value = "${name}" > < br / > age: < input type = "text" th: value = "${age}" > < br / > no. : <input type="text" th:value="${id}"><br/> </body> </html>Copy the code
Start the project, in your browser’s address bar enter, http://localhost:8080/thymeleaf, page results are as follows:
Thymeleaf page template created!
conclusion
This article focuses on how SpringBoot can efficiently create pages using freearker and Thymeleaf templates.
Finally, welcome to pay attention to the public number: 1024 notes, access to more relevant articles, but also free access to massive learning resources (including video, source code, documentation)!
Related recommendations:
-
Spring annotation (3) : @scope sets the scope of the component
-
Spring is worth your collection!!
-
Spring annotation (7) : Assign attributes to beans using @value
-
Spring Note (5) : Four ways a container can register a component
-
Spring Note (6) : Four ways to customize initialization and destruction methods during the Bean’s life cycle