Gab*_*rat 5 java spring thymeleaf spring-webflux
我正在尝试将 thymeleaf 形式与单值反应数据流一起使用。使用 spring boot 2.1.3、thymeleaf 3.0.11 和 webflux。我的 POJO 是:
@Getter
@Setter
@NoArgsConstructor
public class RecipeCommand {
private String id;
private String description;
}
Run Code Online (Sandbox Code Playgroud)
控制器是:
@GetMapping("/recipe/{id}/update")
public String updateRecipe(Model model, @PathVariable String id){
Mono<RecipeCommand> recipeCommandMono = recipeService.getRecipeCommandById(id);
model.addAttribute("recipe", recipeCommandMono);
return "recipes/recipeform";
}
Run Code Online (Sandbox Code Playgroud)
其中 recipes/recipeform 是百里香叶形式以更新配方:
<form th:object="${recipe}" th:action="@{/post}" method="post">
<input type="hidden" th:field="*{id}"/>
Run Code Online (Sandbox Code Playgroud)
应用程序运行但在尝试更新配方时出现以下错误:
引起:org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [reactor.core.publisher.MonoOnAssembly]: Bean 属性 'id' 不可读或具有无效的 getter 方法: getter 匹配 setter 的参数类型?
任何想法如何使用百里香叶形式与反应流。
聚甲醛:
<?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>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gabriel</groupId>
<artifactId>spring-recipe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-recipe</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>popper.js</artifactId>
<version>1.14.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
您正在尝试将Mono传递给您的模板。相反,您可以使用IReactiveDataDriverContextVariable对象包装Mono。IReactiveDataDriverContextVariable 与Flux一起使用,因此您应该使用Flux()方法将Mono转换为Flux。
@GetMapping("/recipe/{id}/update")
public String updateRecipe(Model model, @PathVariable String id){
Mono<RecipeCommand> recipeCommandMono = recipeService.getRecipeCommandById(id);
IReactiveDataDriverContextVariable recipe = new ReactiveDataDriverContextVariable(recipeCommandMono.flux(), 1);
model.addAttribute("recipes", recipe);
return "recipes/recipeform";
}
Run Code Online (Sandbox Code Playgroud)
更改百里香部分(变量食谱现在是异步列表)
<div th:each="recipe : ${recipes}">
<form th:object="${recipe}" th:action="@{/post}" method="post">
<input type="hidden" th:field="*{id}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
678 次 |
| 最近记录: |