代码 订购
public class Order {
private String id;
private BigDecimal amount;
//get set constructor constructors
}
IndexController
@Controller
public class IndexController {
@GetMapping
public String index() {
return "index";
}
}
OrderController
@Controller
@RequestMapping("/orders")
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
@GetMapping
public Mono<String> list(Model model) {
var orders = orderService.orders();
model.addAttribute("orders", orders);
return Mono.just("orders/list");
}
}
OrderGenerator
public class OrderGenerator {
public Order generate() {
var amount = ThreadLocalRandom.current().nextDouble(1000.00);
return new Order(UUID.randomUUID().toString(), BigDecimal.valueOf(amount));
}
}
OrderService
@Service
public class OrderService {
private final Map<String, Order> orders = new ConcurrentHashMap<>(10);
@PostConstruct
public void init() {
var generator = new OrderGenerator();
for (int i = 0; i < 25; i++) {
var order = generator.generate();
orders.put(order.getId(), order);
}
}
public Mono<Order> findById(String id) {
return Mono.justOrEmpty(orders.get(id));
}
public Mono<Order> save(Mono<Order> order) {
return order.map(this::save);
}
private Order save(Order order) {
orders.put(order.getId(), order);
return order;
}
public Flux<Order> orders() {
return Flux.fromIterable(orders.values()).delayElements(Duration.ofMillis(128));
}
}
html
<body>
<h1>Orders</h1>
<table>
<thead>
<tr>
<th></th>
<th>Id</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr th:each="order : ${orders}">
<td th:text="${order.id}"></td>
<td th:text="${#numbers.formatCurrency(order.amount)}"
style="text-align: right"></td>
</tr>
</tbody>
</table>
</body>
错误
模板解析期间发生错误(模板:“类路径资源[templates / orders / list.html]”)
异常评估SpringEL表达式:“ order.id”(模板:“ orders / list”-第19行,第9行)
EL1008E:在类型为'reactor.core.publisher.FluxOnAssembly'的对象上找不到属性或字段'id'-可能不是公共的或无效的吗?
我没有解决问题的方法,希望您能帮助我。谢谢您的答复。
答案 0 :(得分:0)
您需要将诸如Flux和Mono的反应性原语添加为IReactiveDataDriverContextVariable
。
IReactiveDataDriverContextVariable reactiveDataDrivenMode =
new ReactiveDataDriverContextVariable(orders);
model.addAttribute("orders", reactiveDataDrivenMode);
答案 1 :(得分:0)
检查您使用的是spring-boot-starter-webflux而不是spring-boot-starter-web