SpringBoot integrates freemarker
1. Add dependency: springboot is basically seamless. Basically, you only need to add the corresponding dependency, without or with a small amount of configuration
Note: the creation of springboot project is not described here
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2. Create controller
/** * springboot Integrate freemarker */ @Controller public class UserController { @GetMapping("/user") public String user(Model model){ List<User> list = new ArrayList<>(); for (int i = 0 ; i < 10 ; i++){ User user = new User(); user.setId(i); user.setUsername("shangushenlong>>"+i); user.setAge(i); list.add(user); } model.addAttribute("users",list); return "userInfo"; } }
3. Create bean
public class User { private Integer id; private String username; private Integer age; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", age=" + age + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
4. Create freemarker file (use idea to create a file with suffix. ftl)
Create the userInfo.ftl file in the Templates folder. If we don't change the configuration in application.properties, springboot will automatically go to templates to find related files by default
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <tr> <td>id</td> <td>full name</td> <td>Age</td> </tr> <#list users as user> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.age}</td> </tr> </#list> </table> </body> </html>
be careful:
- freemarker is just a template engine. You can think of it as another html file similar to jsp syntax
- Most likely error: the route configuration is correct, but 404 error occurs. The possible reason is that you need to configure application.properties in the configuration file and specify the suffix. ftl of the file, because the default suffix of springboot is probably not this
spring.freemarker.suffix=.ftl
5. The above is the basic steps of integrated configuration
Spring boot integrates thmeleaf
1. Add dependency, create controller, bean, etc. as above, the code is as follows
2. Add dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
3. Create controller
/** * springboot Integrate thymeleaf */ @Controller public class StudentController { @GetMapping("/student") public String stu(Model model){ ArrayList<Student> students = new ArrayList<>(); for (int i = 0 ;i < 10 ; i++) { Student student = new Student(); student.setId(i); student.setUsername("shanggushenlong>>" + i); student.setAge(String.valueOf(i)); students.add(student); } model.addAttribute("students",students); return "student"; } }
4. Create bean
public class Student { private Integer id; private String username; private String age; @Override public String toString() { return "StudentController{" + "id=" + id + ", username='" + username + '\'' + ", age='" + age + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
5. Create html file (thmeleaf template engine suffix. html)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns:th="http://www.w3.org"> <head> <title>Title</title> </head> <body> <table border="1"> <tr> <td>id</td> <td>username</td> <td>age</td> </tr> <tr th:each="stu : ${students}"> <td th:text="${stu.id}"></td> <td th:text="${stu.username}"></td> <td th:text="${stu.age}"></td> </tr> </table> </body> </html>
I hope I can keep my original intention, keep writing the article and grow with you
github address: https://github.com/shanggushenlong/springboot-demo