mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
WebFlux 整合 Thymeleaf
This commit is contained in:
48
springboot-webflux-4-thymeleaf/pom.xml
Executable file
48
springboot-webflux-4-thymeleaf/pom.xml
Executable file
@@ -0,0 +1,48 @@
|
|||||||
|
<?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>
|
||||||
|
|
||||||
|
<groupId>springboot</groupId>
|
||||||
|
<artifactId>springboot-webflux-4-thymeleaf</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>springboot-webflux-4-thymeleaf :: Spring Boot WebFlux 整合 Thymeleaf</name>
|
||||||
|
|
||||||
|
<!-- Spring Boot 启动父依赖 -->
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.0.1.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- Spring Boot Web Flux 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 模板引擎 Thymeleaf 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Spring Boot Test 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Junit -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.spring.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring Boot 应用启动类
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
// Spring Boot 应用的标识
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 程序启动入口
|
||||||
|
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
|
||||||
|
SpringApplication.run(Application.class,args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package org.spring.springboot.dao;
|
||||||
|
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class CityRepository {
|
||||||
|
|
||||||
|
private ConcurrentMap<Long, City> repository = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private static final AtomicLong idGenerator = new AtomicLong(0);
|
||||||
|
|
||||||
|
public Long save(City city) {
|
||||||
|
Long id = idGenerator.incrementAndGet();
|
||||||
|
city.setId(id);
|
||||||
|
repository.put(id, city);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<City> findAll() {
|
||||||
|
return repository.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public City findCityById(Long id) {
|
||||||
|
return repository.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long updateCity(City city) {
|
||||||
|
repository.put(city.getId(), city);
|
||||||
|
return city.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long deleteCity(Long id) {
|
||||||
|
repository.remove(id);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package org.spring.springboot.domain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市实体类
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class City {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市编号
|
||||||
|
*/
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 省份编号
|
||||||
|
*/
|
||||||
|
private Long provinceId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市名称
|
||||||
|
*/
|
||||||
|
private String cityName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 描述
|
||||||
|
*/
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getProvinceId() {
|
||||||
|
return provinceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProvinceId(Long provinceId) {
|
||||||
|
this.provinceId = provinceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCityName() {
|
||||||
|
return cityName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCityName(String cityName) {
|
||||||
|
this.cityName = cityName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.spring.springboot.handler;
|
||||||
|
|
||||||
|
import org.spring.springboot.dao.CityRepository;
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CityHandler {
|
||||||
|
|
||||||
|
private final CityRepository cityRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public CityHandler(CityRepository cityRepository) {
|
||||||
|
this.cityRepository = cityRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<Long> save(City city) {
|
||||||
|
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.save(city)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<City> findCityById(Long id) {
|
||||||
|
return Mono.justOrEmpty(cityRepository.findCityById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Flux<City> findAllCity() {
|
||||||
|
return Flux.fromIterable(cityRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<Long> modifyCity(City city) {
|
||||||
|
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.updateCity(city)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<Long> deleteCity(Long id) {
|
||||||
|
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.deleteCity(id)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.spring.springboot.webflux.controller;
|
||||||
|
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.spring.springboot.handler.CityHandler;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(value = "/city")
|
||||||
|
public class CityWebFluxController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CityHandler cityHandler;
|
||||||
|
|
||||||
|
@GetMapping(value = "/{id}")
|
||||||
|
@ResponseBody
|
||||||
|
public Mono<City> findCityById(@PathVariable("id") Long id) {
|
||||||
|
return cityHandler.findCityById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping()
|
||||||
|
@ResponseBody
|
||||||
|
public Flux<City> findAllCity() {
|
||||||
|
return cityHandler.findAllCity();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
@ResponseBody
|
||||||
|
public Mono<Long> saveCity(@RequestBody City city) {
|
||||||
|
return cityHandler.save(city);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping()
|
||||||
|
@ResponseBody
|
||||||
|
public Mono<Long> modifyCity(@RequestBody City city) {
|
||||||
|
return cityHandler.modifyCity(city);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping(value = "/{id}")
|
||||||
|
@ResponseBody
|
||||||
|
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
|
||||||
|
return cityHandler.deleteCity(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String CITY_LIST_PATH_NAME = "cityList";
|
||||||
|
|
||||||
|
@GetMapping("/page/list")
|
||||||
|
public String listPage(final Model model) {
|
||||||
|
final Flux<City> cityFluxList = cityHandler.findAllCity();
|
||||||
|
model.addAttribute("cityList", cityFluxList);
|
||||||
|
return CITY_LIST_PATH_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8"/>
|
||||||
|
<title>城市列表</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<legend>
|
||||||
|
<strong>城市列表</strong>
|
||||||
|
</legend>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>城市编号</th>
|
||||||
|
<th>省份编号</th>
|
||||||
|
<th>名称</th>
|
||||||
|
<th>描述</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="city : ${cityList}">
|
||||||
|
<td th:text="${city.id}"></td>
|
||||||
|
<td th:text="${city.provinceId}"></td>
|
||||||
|
<td th:text="${city.cityName}"></td>
|
||||||
|
<td th:text="${city.description}"></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user