在发布到 Api 之前验证表单输入时出现 Thymeleaf 错误
Thymeleaf error when validating form inputs before posting to Api
这是我收到的错误(注意:错误 被 捕获):
浏览器:Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (forms/frmEntry:75)
STS IDE: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'e' available as request attribute at
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
....
....
GET
方法(显示形式):
@RequestMapping(value="/create", method=RequestMethod.GET)
public String create(Model model){
model.addAttribute("entry", new EntryForm());
return "forms/frmEntry";
}
由于我原来的 POST
方法不起作用,我尝试创建另一个更精简的 POST
方法,只是为了排除与 postToEntity
调用 Api 相关的其他代码.
@PostMapping("/temp")
public String temp(@Valid @ModelAttribute EntryForm e, BindingResult bindingResult){
// returns 1 in debug mode (error for the only field (name) in the form, so OK
// int ct = bindingResult.getErrorCount();
if(bindingResult.hasErrors()){
// this Thymeleaf view/template is reached
return "forms/frmEntry";
}
// dummy Thymeleaf view (doesn't exist)
return "proslodokraja";
}
我的表格:
<form action="/consumer/temp" th:object="${e}" method="post" class="col-md-6">
// in my code, this hidden field is commented out (for now)
<input type="hidden" th:field="*{id}" />
<p>
Name: <input type="text" th:field="*{name}" class="form-control" />
<br/>
<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</span>
</p>
<p>
<input type="submit" value="Submit" class="btn btn-primary" />
<input type="reset" value="Reset" class="btn btn-default" />
</p>
</form>
形式Bean
:
public class EntryForm {
private Long id;
@NotNull
@Length(min=2) // hibernate import!
private String name;
public EntryForm(){ }
public EntryForm(String name) { this.name = name; }
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
问题出在您的模型属性名称中。您将其设置为 "entry",然后您试图访问 frmEntry
表单中的 "e" 模型属性(甚至未设置)。在设置模型属性时将其重命名为 "e" 或在 frmEntry
页面上将其重命名为 "entry" 并在 @ModelAttribute
:
中明确提供名称
<form action="/consumer/temp" th:object="${entry}" method="post" class="col-md-6">
@ModelAttribute("entry") EntryForm e
这是我收到的错误(注意:错误 被 捕获):
浏览器:Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (forms/frmEntry:75)
STS IDE: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'e' available as request attribute at
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
....
....
GET
方法(显示形式):
@RequestMapping(value="/create", method=RequestMethod.GET)
public String create(Model model){
model.addAttribute("entry", new EntryForm());
return "forms/frmEntry";
}
由于我原来的 POST
方法不起作用,我尝试创建另一个更精简的 POST
方法,只是为了排除与 postToEntity
调用 Api 相关的其他代码.
@PostMapping("/temp")
public String temp(@Valid @ModelAttribute EntryForm e, BindingResult bindingResult){
// returns 1 in debug mode (error for the only field (name) in the form, so OK
// int ct = bindingResult.getErrorCount();
if(bindingResult.hasErrors()){
// this Thymeleaf view/template is reached
return "forms/frmEntry";
}
// dummy Thymeleaf view (doesn't exist)
return "proslodokraja";
}
我的表格:
<form action="/consumer/temp" th:object="${e}" method="post" class="col-md-6">
// in my code, this hidden field is commented out (for now)
<input type="hidden" th:field="*{id}" />
<p>
Name: <input type="text" th:field="*{name}" class="form-control" />
<br/>
<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</span>
</p>
<p>
<input type="submit" value="Submit" class="btn btn-primary" />
<input type="reset" value="Reset" class="btn btn-default" />
</p>
</form>
形式Bean
:
public class EntryForm {
private Long id;
@NotNull
@Length(min=2) // hibernate import!
private String name;
public EntryForm(){ }
public EntryForm(String name) { this.name = name; }
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
问题出在您的模型属性名称中。您将其设置为 "entry",然后您试图访问 frmEntry
表单中的 "e" 模型属性(甚至未设置)。在设置模型属性时将其重命名为 "e" 或在 frmEntry
页面上将其重命名为 "entry" 并在 @ModelAttribute
:
<form action="/consumer/temp" th:object="${entry}" method="post" class="col-md-6">
@ModelAttribute("entry") EntryForm e