Skip to content

Commit 86aedc5

Browse files
author
liqiangqiang
committed
WebFlux 整合 Thymeleaf
1 parent 6927d5d commit 86aedc5

File tree

8 files changed

+305
-0
lines changed

8 files changed

+305
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-webflux-4-thymeleaf</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-webflux-4-thymeleaf :: Spring Boot WebFlux 整合 Thymeleaf</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.1.RELEASE</version>
16+
</parent>
17+
18+
<dependencies>
19+
20+
<!-- Spring Boot Web Flux 依赖 -->
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-webflux</artifactId>
24+
</dependency>
25+
26+
<!-- 模板引擎 Thymeleaf 依赖 -->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
30+
</dependency>
31+
32+
33+
<!-- Spring Boot Test 依赖 -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
<!-- Junit -->
41+
<dependency>
42+
<groupId>junit</groupId>
43+
<artifactId>junit</artifactId>
44+
<version>4.12</version>
45+
</dependency>
46+
</dependencies>
47+
48+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
*/
10+
// Spring Boot 应用的标识
11+
@SpringBootApplication
12+
public class Application {
13+
14+
public static void main(String[] args) {
15+
// 程序启动入口
16+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
17+
SpringApplication.run(Application.class,args);
18+
}
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.spring.springboot.dao;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.springframework.stereotype.Repository;
5+
6+
import java.util.Collection;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ConcurrentMap;
9+
import java.util.concurrent.atomic.AtomicLong;
10+
11+
@Repository
12+
public class CityRepository {
13+
14+
private ConcurrentMap<Long, City> repository = new ConcurrentHashMap<>();
15+
16+
private static final AtomicLong idGenerator = new AtomicLong(0);
17+
18+
public Long save(City city) {
19+
Long id = idGenerator.incrementAndGet();
20+
city.setId(id);
21+
repository.put(id, city);
22+
return id;
23+
}
24+
25+
public Collection<City> findAll() {
26+
return repository.values();
27+
}
28+
29+
30+
public City findCityById(Long id) {
31+
return repository.get(id);
32+
}
33+
34+
public Long updateCity(City city) {
35+
repository.put(city.getId(), city);
36+
return city.getId();
37+
}
38+
39+
public Long deleteCity(Long id) {
40+
repository.remove(id);
41+
return id;
42+
}
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.spring.springboot.domain;
2+
3+
/**
4+
* 城市实体类
5+
*
6+
*/
7+
public class City {
8+
9+
/**
10+
* 城市编号
11+
*/
12+
private Long id;
13+
14+
/**
15+
* 省份编号
16+
*/
17+
private Long provinceId;
18+
19+
/**
20+
* 城市名称
21+
*/
22+
private String cityName;
23+
24+
/**
25+
* 描述
26+
*/
27+
private String description;
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public Long getProvinceId() {
38+
return provinceId;
39+
}
40+
41+
public void setProvinceId(Long provinceId) {
42+
this.provinceId = provinceId;
43+
}
44+
45+
public String getCityName() {
46+
return cityName;
47+
}
48+
49+
public void setCityName(String cityName) {
50+
this.cityName = cityName;
51+
}
52+
53+
public String getDescription() {
54+
return description;
55+
}
56+
57+
public void setDescription(String description) {
58+
this.description = description;
59+
}
60+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.spring.springboot.handler;
2+
3+
import org.spring.springboot.dao.CityRepository;
4+
import org.spring.springboot.domain.City;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Component;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
@Component
11+
public class CityHandler {
12+
13+
private final CityRepository cityRepository;
14+
15+
@Autowired
16+
public CityHandler(CityRepository cityRepository) {
17+
this.cityRepository = cityRepository;
18+
}
19+
20+
public Mono<Long> save(City city) {
21+
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.save(city)));
22+
}
23+
24+
public Mono<City> findCityById(Long id) {
25+
return Mono.justOrEmpty(cityRepository.findCityById(id));
26+
}
27+
28+
public Flux<City> findAllCity() {
29+
return Flux.fromIterable(cityRepository.findAll());
30+
}
31+
32+
public Mono<Long> modifyCity(City city) {
33+
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.updateCity(city)));
34+
}
35+
36+
public Mono<Long> deleteCity(Long id) {
37+
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.deleteCity(id)));
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.spring.springboot.webflux.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.handler.CityHandler;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.*;
9+
import reactor.core.publisher.Flux;
10+
import reactor.core.publisher.Mono;
11+
12+
@Controller
13+
@RequestMapping(value = "/city")
14+
public class CityWebFluxController {
15+
16+
@Autowired
17+
private CityHandler cityHandler;
18+
19+
@GetMapping(value = "/{id}")
20+
@ResponseBody
21+
public Mono<City> findCityById(@PathVariable("id") Long id) {
22+
return cityHandler.findCityById(id);
23+
}
24+
25+
@GetMapping()
26+
@ResponseBody
27+
public Flux<City> findAllCity() {
28+
return cityHandler.findAllCity();
29+
}
30+
31+
@PostMapping()
32+
@ResponseBody
33+
public Mono<Long> saveCity(@RequestBody City city) {
34+
return cityHandler.save(city);
35+
}
36+
37+
@PutMapping()
38+
@ResponseBody
39+
public Mono<Long> modifyCity(@RequestBody City city) {
40+
return cityHandler.modifyCity(city);
41+
}
42+
43+
@DeleteMapping(value = "/{id}")
44+
@ResponseBody
45+
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
46+
return cityHandler.deleteCity(id);
47+
}
48+
49+
private static final String CITY_LIST_PATH_NAME = "cityList";
50+
51+
@GetMapping("/page/list")
52+
public String listPage(final Model model) {
53+
final Flux<City> cityFluxList = cityHandler.findAllCity();
54+
model.addAttribute("cityList", cityFluxList);
55+
return CITY_LIST_PATH_NAME;
56+
}
57+
58+
}

springboot-webflux-4-thymeleaf/src/main/resources/application.properties

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-CN">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>城市列表</title>
6+
</head>
7+
8+
<body>
9+
10+
<div>
11+
12+
13+
<table>
14+
<legend>
15+
<strong>城市列表</strong>
16+
</legend>
17+
<thead>
18+
<tr>
19+
<th>城市编号</th>
20+
<th>省份编号</th>
21+
<th>名称</th>
22+
<th>描述</th>
23+
</tr>
24+
</thead>
25+
<tbody>
26+
<tr th:each="city : ${cityList}">
27+
<td th:text="${city.id}"></td>
28+
<td th:text="${city.provinceId}"></td>
29+
<td th:text="${city.cityName}"></td>
30+
<td th:text="${city.description}"></td>
31+
</tr>
32+
</tbody>
33+
</table>
34+
35+
</div>
36+
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)