The third day of micro HR: SpringBoot integrates Freemarker

Let's talk about how SpringBoot integrates Freemarker: You need to check web and Freemarker when creating a springboot project After the project...

Let's talk about how SpringBoot integrates Freemarker:

You need to check web and Freemarker when creating a springboot project

After the project is started successfully, press shift twice to view the FreeMarkerAutoConfiguration class, which is the automatic configuration class of FreeMarket

Let's talk about what's done in FreeMarkerAutoConfiguration
Check template location:

Get template location:

The most important ones are:

@ConditionalOnClass()

The meaning of this code is: the following methods will take effect when Freemarker is introduced

And this dependency has been automatically configured in pom.xml file

@ConditionalOnClass()

The above comment is very important in Freemarker.

Look again:

@Import()

This code allows FreeMarker to be used in different environments

@EnableConfigurationProperties()

Press and hold ctrl to click into FreeMarkerProperties:
Type safe property injection:

On the surface of this sentence, we can configure such a property as spring.freemarker in application.properties

Default template load path:

The template suffix is. ftl:

Next, we will use a specific example to illustrate how Freemarker uses:
1. Create entity class user

package org.javaboy.freemarker.bean; public class User { private Long id; private String username; private String address; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", address='" + address + '\'' + '}'; } }

2. control class

package org.javaboy.freemarker.controller; import org.javaboy.freemarker.bean.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.ArrayList; import java.util.List; @Controller public class UserController { @GetMapping("/user") public String user(Model model) { List<User> users = new ArrayList<>(); for (int i = 0; i < 10; i++) { User user = new User(); user.setId((long) i); user.setUsername("javaboy>>>" + i); user.setAddress("www.javaboy.org>>>" + i); users.add(user); } model.addAttribute("users", users); return "user"; } }

3. Create Freemarker template under template package
Click Edit File Templates

Edit the template name, suffix and content respectively

4. Create freemarker file

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <tr> <td>number</td> <td>User name</td> <td>User address</td> </tr> <#list users as u> <tr> <td>$</td> <td>$</td> <td>$</td> </tr> </#list> </table> </body> </html>

Display the added content of control class in the form of table

5. boot
Visit: http://localhost:8080/user

We can also configure freemarker in the properties file as follows:

#Template encoding format, default is UTF-8 spring.freemarker.charset=UTF-8 #Define the content type of the template spring.freemarker.content-type=text/html #Open cache or not spring.freemarker.cache=false #Configure template suffix spring.freemarker.suffix=.ftl
YRZ-James 264 original articles published, 23 praised, 20000 visitors+ Private letter follow

13 January 2020, 04:43 | Views: 1706

Add new comment

For adding a comment, please log in
or create account

0 comments