带有百里香叶的对象没有绑定资源

时间:2021-07-01 08:51:41

标签: thymeleaf

给定:

  • 带有控制器方法的反应式链

    @PostMapping("/add")
    public Mono<String> addSkill(@ModelAttribute Skill skil) {
      log.info("skill "+ skill.getName() + " has been added!");
      return service.add(skill)
              .then(Mono.just(TEMPLATE));
    
    
    @GetMapping("")
    public Mono<String> getAll(Model model) {
      IReactiveDataDriverContextVariable driverContextVariable = new ReactiveDataDriverContextVariable(service.findAll(), 100);
      model.addAttribute("skills", driverContextVariable);
      return Mono.just(TEMPLATE);}
    
  • 形式:

<div class="row">
        <div id="title">
            <h1>Reactive java skills</h1>
        </div>
        <table id="allSkills" class="table table-striped">
            <thead>
            <tr>
                <th>Name</th>
                <th>Level</th>
                <th>Priority</th>
                <th>Skill Group</th>
            </tr>
            </thead>
            <tbody>
            <tr class="skills" data-th-each="skill : ${skills}">
                <td>[[${skill.name}]]</td>
                <td>[[${skill.level}]]</td>
                <td>[[${skill.priority}]]</td>
                <td>[[${skill.skillGroupName}]]</td>
            </tr>
            </tbody>
        </table>
    </div>
        <div>
        <h4>add skill</h4>
        <form th:action="@{/skill/add}" th:object="${skill}" method="post">
            <input type="text" th:field="*{skill.name}" placeholder="choose skill name"/>
            <input type="text" th:field="*{skill.level}" placeholder="choose skill level"/>
            <input type="text" th:field="*{skill.priority}" placeholder="choose skill priority"/>
            <input type="text" th:field="*{skill.skillGroupName}" placeholder="choose skill priority"/>
            <input type="submit" value="Submit" /> <input type="reset" value="Reset" />
        </form>
    </div>

我的目标很简单:通过提交表单添加一个新实体并呈现相同的内容 html 显示所有实体。据我了解 Reactive Thyme 的概念,这里没有 @PathVariable 由于它背后的代码阻塞,所以我使用 @ModelAttribute 为新技能传递参数。但是,当我为控制器请求基本路径时,它会呈现所有技能并抛出:

<块引用>

java.lang.IllegalStateException: 既不是 BindingResult 也不是普通的 可用作请求属性的 bean 名称“技能”的目标对象

我不知道如何在呈现页面时让 thymeleaf 忽略 ${skill} 参数并仅在提交表单时使用它。

PS - 我也没有找到反应性百里香叶的官方文档,如果有人分享链接会很棒。

1 个答案:

答案 0 :(得分:0)

基本上,我应该以反应方式填充所有属性。所以,问题出在我的 getAll() 方法上,它应该能够填充模板 html 中声明的所有百里香叶对象。我为了更好地记忆原因而提出的方法的工作版本:

@GetMapping("")
public Mono<String> getAll(Model model) {
    log.info("no attr =- no future");
    return Mono.just(model)
            .flatMap(m -> {
                m.addAttribute("skills", new ReactiveDataDriverContextVariable(service.findAll(), 10));
                m.addAttribute("skill", new Skill());
                return Mono.just(TEMPLATE);

            })
          .doOnNext(sink -> System.out.println(sink));
相关问题