@@ Mapping无效:BeanResult'XXX'的BindingResult或普通目标对象都不能用作请求属性

问题描述 投票:0回答:2

我是thymeleafSpring-Boot的新手,我按照此tutorial创建了一个提交表单,我做了完全一样的事情,但是出现以下异常:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'conjugation' available as request attribute

[有人知道这是我的Conjugation类,可以告诉我我的代码有什么问题

public class Conjugation {
    private String v;

    public String getV() {
        return this.v;
    }

    public void setV(String v) {
        this.verb = v;
    }

}

这是我在控制器中使用的内容

//The Controller
@GetMapping("/conjugate")
  public String vForm(Model model) {
    model.addAttribute("conjugate", new Conjugation());
    return "conjugate";
  }

@PostMapping("/conjugate")
  public String vbSubmit(@ModelAttribute Conjugation conjugate) {
    System.out.println("-->"+conjugate.getV());
    return "index";
  }

这是我的index.html文件:

<form action="#" th:action="@{/conjugate}" th:object="${conjugation}" method="post">
    <div class="cell">
        V
    </div>
    <div class="cell">
        <input class="cell" type="text"  th:field="*{v}" /> 
    </div>
    <div class="cell">
        <input type="submit" value="Submit" />
    </div>
spring-boot thymeleaf
2个回答
0
投票
[th:value替换为th:field

您的变量v的长度应大于3。我不知道为什么这样。但是我遇到了这种情况,即命名变量。


0
投票
您添加名为conjugate的属性:

model.addAttribute("conjugate", new Conjugation());

但是在您的html文件中却说conjugation

th:object="${conjugation}"

如果将当前的model.addAttribute替换为:,这将起作用:

model.addAttribute("conjugation", new Conjugation());

© www.soinside.com 2019 - 2024. All rights reserved.