目录
理论
演示及源码
理论
后端通过@GetMapping获取数据,把数据存储在Model中,前端使用模板引擎机进行获取即可。
在@GetMapping中填写请求信息;
Model再通过addAttribute与前端进行交互!
演示及源码
程序运行截图如下:
项目结构如下:
源码如下:
MyMvcConfig.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package 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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package 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<People> peoples = peopleData.getAll(); model.addAttribute("index" , peoples); return "index"; } }
PeopleData.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package 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<Integer, People> peoples = null; static { peoples = new HashMap<Integer, People>(); peoples.put(1000, new People(1000, "闰土", "1234567@163.com", 1, new Date())); peoples.put(1001, new People(1001, "妹爷", "110@163.com", 1, new Date())); peoples.put(1002, new People(1002, "球球", "120@163.com", 0, new Date())); peoples.put(1003, new People(1003, "猪小明", "119@163.com", 1, new Date())); peoples.put(1004, new People(1004, "米线", "911@163.com", 0, new Date())); peoples.put(1005, new People(1005, "腿腿", "12306@163.com", 0, new Date())); } public Collection<People> getAll(){ return peoples.values(); } }
People.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72package 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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>呵呵</title> <link href="#" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet"> </head> <body> <div class="table-responsive"> <table class="table table-striped table-sm"> <thead> <tr> <th>编号</th> <th>姓名</th> <th>邮箱</th> <th>性别</th> <th>生日</th> <th>操作</th> </tr> </thead> <tbody> <tr th:each="people:${index}"> <td th:text="${people.id}"></td> <td>[[${people.name}]]</td> <td th:text="${people.email}"></td> <td th:text="${people.gender} == 1 ? '男' : '女'"></td> <td th:text="${#dates.format(people.birth, 'yyy-MMM-ddd HH:mm')}"></td> <td> <button class="btn btn-sm btn-primary">编辑</button> <button class="btn btn-sm btn-danger">删除</button> </td> </tr> </tbody> </table> </div> </body> </html>
application.properties
复制代码
1spring.thymeleaf.cache=false
porn.xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.19.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.loginWebDemo</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>loginWeb</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--引入jquery-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency> <!--引入bootstrap--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
最后
以上就是高高钢铁侠最近收集整理的关于Spring Boot中表格的请求以及表格界面的显示 理论演示及源码的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复