我是thymeleaf
和Spring-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>
th:value
替换为th:field
和
您的变量v
的长度应大于3。我不知道为什么这样。但是我遇到了这种情况,即命名变量。
conjugate
的属性:model.addAttribute("conjugate", new Conjugation());
但是在您的html文件中却说conjugation
th:object="${conjugation}"
如果将当前的model.addAttribute
替换为:,这将起作用:
model.addAttribute("conjugation", new Conjugation());