Thymeleaf webflux:仅允许将一个数据驱动变量指定为模型属性,但至少已识别两个

Dou*_*uma 6 java spring reactive-programming thymeleaf spring-webflux

我正在尝试创建一个仪表板应用程序,其中多个小部件通过 SSE 获取更新。我的 DashboardController 看起来像:

public class DashboardController
{
    private WidgetService widgetService;

    public DashboardController(WidgetService widgetService)
    {
        this.widgetService = widgetService;
    }

    @GetMapping("/")
    public String index(final Model model)
    {
        for(WidgetInterface<?> widget : widgetService.getAll()) {
            model.addAttribute("widget-data-driver-" + widget.getIdentifier(),
                    new ReactiveDataDriverContextVariable(widget.getInitialData(), 100));
        }
        model.addAttribute("widgets", widgetService.getAll());
        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

和我的小部件界面:

public interface WidgetInterface<T>
{
    public String getIdentifier();
    public String getTitle();
    public String getStreamEndpoint();
    public Flux<T> getInitialData();
    public Map<String, String> getColumns();
    public String getThymeLeafFraction();
    public Date getLastItemDate();
    public Flux<T> getItemsUpdatedSince(Date date);
}
Run Code Online (Sandbox Code Playgroud)

一个小部件(来自 WidgetService)一切正常。我正在尝试将ReactiveDataDriverContextVariable每个小部件的 a 传递给 Thymeleaf。但我收到以下错误:

org.thymeleaf.exceptions.TemplateProcessingException: Only one data-driver variable is allowed to be specified as a model attribute, but at least two have been identified: 'widget-data-driver-nufeed' and 'widget-data-driver-weatherapi'
Run Code Online (Sandbox Code Playgroud)

我有两个活动小部件,一个是 RSS feed,一个实现天气 API。我理解该错误,但如何为多个小部件设置反应变量?

根据 Angelos 评论更新答案(2022)

我尝试将 DashboardController.index 方法替换为:

org.thymeleaf.exceptions.TemplateProcessingException: Only one data-driver variable is allowed to be specified as a model attribute, but at least two have been identified: 'widget-data-driver-nufeed' and 'widget-data-driver-weatherapi'
Run Code Online (Sandbox Code Playgroud)

这是有效的,除了现在我无法迭代 thymeleaf 中的 widget.getInitialData() (这是如上所述的分数文件):

GetMapping("/")
public String index(final Model model, Authentication authentication)
{
     setAuthentication(authentication);
     Flux<WidgetInterface<?>> flux = Flux.just(widgetService.getAll().toArray(new WidgetInterface<?>[0]));
     model.addAttribute("widgets", new ReactiveDataDriverContextVariable(flux));
     return "index";
}
Run Code Online (Sandbox Code Playgroud)

这给出了这个错误:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "item.id" (template: "art_fraction" - line 1, col 56)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292) ~[thymeleaf-spring5-3.0.15.RELEASE.jar:3.0.15.RELEASE]
Run Code Online (Sandbox Code Playgroud)

Ang*_*ata 0

我会让你的模板代码更简单一些。我会这样改变它:

Java端

GetMapping("/")
public String index(final Model model, Authentication authentication)
{
     setAuthentication(authentication);
     Flux<WidgetInterface<?>> flux = Flux.just(widgetService.getAll().toArray(new WidgetInterface<?>[0]));
     model.addAttribute("widgets", new ReactiveDataDriverContextVariable(flux));
     return "index";
}
Run Code Online (Sandbox Code Playgroud)

模板面

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Image</th>
                    <th>Identifier</th>
                </tr>
            </thead>
            <tbody>
                <tr data-th-each="myWidget : ${widgets}">
                    <td>[[${myWidget.id}]]</td>
                    <td>[[${myWidget.image}]]</td>
                    <td>[[${myWidget.identifier}]]</td>
                </tr>
            </tbody>
        </table>
Run Code Online (Sandbox Code Playgroud)

显然这是一个示例...根据您的情况进行调整

安吉洛