1.首先新建项目
项目这就创建好了,下面配置pom.xml文件
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- ThymeLeaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.实现功能
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<Student> 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页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<table border="1">
<tr>
<td>ID</td>
<td>名字</td>
<td>学号</td>
<td>性别</td>
</tr>
<tr th:each="student:${studentList}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.num}"></td>
<td th:text="${student.sex}==0?'女':'男'"></td>
</tr>
</table>
</body>
</html>
启动好了:(Springboot中自带tomcat,端口默认8080)
html页面显示: