目录
理论
演示及源码
后端通过@GetMapping获取数据,把数据存储在Model中,前端使用模板引擎机进行获取即可。
在@GetMapping中填写请求信息;
Model再通过addAttribute与前端进行交互!
程序运行截图如下:
项目结构如下:
源码如下:
MyMvcConfig.java
package showtabledata.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter{
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
};
return adapter;
}
}
PeopleController.java
package showtabledata.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import showtabledata.demo.data.PeopleData;
import showtabledata.demo.entities.People;
import java.util.Collection;
@Controller
public class PeopleController {
@Autowired
PeopleData peopleData;
//查询并返回页面
@GetMapping({"/index", "/"})
public String list(Model model){
Collection peoples = peopleData.getAll();
model.addAttribute("index" , peoples);
return "index";
}
}
PeopleData.java
package showtabledata.demo.data;
import org.springframework.stereotype.Repository;
import showtabledata.demo.entities.People;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Repository
public class PeopleData {
private static Map peoples = null;
static {
peoples = new HashMap();
peoples.put(1000, new People(1000, "闰土", "[email protected]", 1, new Date()));
peoples.put(1001, new People(1001, "妹爷", "[email protected]", 1, new Date()));
peoples.put(1002, new People(1002, "球球", "[email protected]", 0, new Date()));
peoples.put(1003, new People(1003, "猪小明", "[email protected]", 1, new Date()));
peoples.put(1004, new People(1004, "米线", "[email protected]", 0, new Date()));
peoples.put(1005, new People(1005, "腿腿", "[email protected]", 0, new Date()));
}
public Collection getAll(){
return peoples.values();
}
}
People.java
package showtabledata.demo.entities;
import java.util.Date;
public class People {
private Integer id;
private String name;
private String email;
private Integer gender;
private Date birth;
public People(Integer id, String name, String email, Integer gender, Date birth) {
this.id = id;
this.name = name;
this.email = email;
this.gender = gender;
this.birth = birth;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", birth=" + birth +
'}';
}
}
index.html
呵呵
编号
姓名
邮箱
性别
生日
操作
[[${people.name}]]
application.properties
spring.thymeleaf.cache=false
porn.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.19.RELEASE
com.loginWebDemo
demo
0.0.1-SNAPSHOT
loginWeb
Demo project for Spring Boot
1.8
3.0.9.RELEASE
2.2.2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.webjars
jquery
3.3.1
org.webjars
bootstrap
4.0.0
org.springframework.boot
spring-boot-maven-plugin