spring boot框架与thymeleaf模板引擎的整合

1.首先新建项目
spring boot框架与thymeleaf模板引擎的整合_第1张图片
spring boot框架与thymeleaf模板引擎的整合_第2张图片
spring boot框架与thymeleaf模板引擎的整合_第3张图片
spring boot框架与thymeleaf模板引擎的整合_第4张图片
spring boot框架与thymeleaf模板引擎的整合_第5张图片

项目这就创建好了,下面配置pom.xml文件

4.0.0

    org.springframework.boot
    spring-boot-starter-parent
    2.1.5.RELEASE
     

com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot


    1.8



    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    




    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

2.实现功能
spring boot框架与thymeleaf模板引擎的整合_第6张图片
student实体类;

public class Student {
private int id;
private String name;
private String num;
private int sex;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNum() {
    return num;
}

public void setNum(String num) {
    this.num = num;
}

public int getSex() {
    return sex;
}

public void setSex(int sex) {
    this.sex = sex;
}

public Student(int id, String name, String num, int sex) {
    this.id = id;
    this.name = name;
    this.num = num;
    this.sex = sex;
}


@Override
public String toString() {
    return "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", num='" + num + '\'' +
            ", sex=" + sex +
            '}';
}

studentController类:

package com.example.demo.student.controller;
import com.example.demo.student.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("stu")
public class StudentController {

@RequestMapping
public String list(Model model)
{
    List  list = new ArrayList<>();


    list.add(new Student(1,"张三","20141",1));
    list.add(new Student(2,"李四","20142",0));
    list.add(new Student(3,"王五","20143",0));
    list.add(new Student(4,"还是","20144",1));
    model.addAttribute("studentList",list);
    return "stu";
}

启动文件:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

html页面:











ID 名字 学号 性别

启动好了:(Springboot中自带tomcat,端口默认8080)
spring boot框架与thymeleaf模板引擎的整合_第7张图片
html页面显示:
spring boot框架与thymeleaf模板引擎的整合_第8张图片

你可能感兴趣的:(springboot,thymeleaf,模板引擎,框架,java,springboot,thymeleaf,框架)