SpringBoot基础实战系列(一)整合视图

下一篇: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

posted @ 2020-05-13 21:53  上古神龙  阅读(1164)  评论(0编辑  收藏  举报