SpringBoot基础实战系列(一)整合视图
2020-06-06 07:00
197 查看
SpringBoot基础实战系列(一)整合视图
- SpringBoot整合freemarker
- 1.添加依赖:springboot基本上是无缝衔接,基本上只需要添加对应的依赖,不需要或者做很少量的配置即可
- 2.创建controller
- 3.创建bean
- 4.创建freemarker文件(使用idea创建后缀名 .ftl 的文件)
- 5.以上就是基本的整合配置步骤
- 1.添加依赖,创建controller,bean等,和上面一样,代码如下
- 2. 添加依赖
- 3.创建controller
- 4.创建bean
- 5.创建html文件(thymeleaf模板引擎后缀名 `.html`)
下一篇:SpringBoot基础实战系列(二)springboot解析json与HttpMessageConverter
SpringBoot整合freemarker
1.添加依赖:springboot基本上是无缝衔接,基本上只需要添加对应的依赖,不需要或者做很少量的配置即可
注:对于springboot项目的创建此处不做说明
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.创建controller
/** * springboot整合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.创建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.创建freemarker文件(使用idea创建后缀名 .ftl 的文件)
在
templates文件夹下创建
userInfo.ftl文件,如果我们不在
application.properties更改配置,则springboot会默认自动去
templates寻找相关的文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1"> <tr> <td>id</td> <td>姓名</td> <td>年龄</td> </tr> <#list users as user> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.age}</td> </tr> </#list> </table> </body> </html>
注意:
freemarker
只是一种模板引擎,你可以将其当成另外一种很类似于jsp
的语法的html
文件- 很可能出现的错误:路由配置正确,但是就是出现
404错误
,可能原因是需要在配置文件中配置application.properties
,指定文件的后缀名.ftl
,因为springboot默认的后缀名很可能不是这个
spring.freemarker.suffix=.ftl
5.以上就是基本的整合配置步骤
SpringBoot整合thymeleaf
1.添加依赖,创建controller,bean等,和上面一样,代码如下
2. 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
3.创建controller
/** * springboot整合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.创建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.创建html文件(thymeleaf模板引擎后缀名 .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>
希望自己能一直保持初衷,文章一直写下去,和大家一起成长
本系列代码github地址:https://github.com/shanggushenlong/springboot-demo
相关文章推荐
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- [Spring Boot实战系列] - No.2 Spring boot 整合Spring Security用户管理和用户权限管理
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- rabbitmq实战系列4--springboot整合rabbitmq实现限流与并发
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- rabbitmq实战系列3--springboot整合rabbitmq实现优先级队列
- SpringBoot基础实战系列(二)springboot解析json与HttpMessageConverter
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- rabbitmq实战系列2--springboot整合rabbitmq的插件实现延迟队列(2)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)