【发布时间】:2016-08-12 02:41:25
【问题描述】:
据我所知,这是正确设置的,但我收到以下错误:
java.lang.IllegalStateException: Neither BindingResult nor plain target
object for bean name 'person' available as request attribute
表格
<form action="#" th:action="@{/person}" th:object="${person}" method="post" th:required="required">
<input type="text" th:field="*{subject}" class="contact col-md-6" placeholder="Name *" th:required="required"/>
<input type="text" th:field="*{name}" class="contact col-md-6" placeholder="Name *" th:required="required"/>
<input type="text" th:field="*{lastName}" class="contact col-md-6" placeholder="Name *" th:required="required"/>
<input type="email" th:field="*{email}" class="contact noMarr col-md-6" placeholder="E-mail address *" th:required="required"/>
<textarea name="comment" class="contact col-md-12" th:field="*{message}" placeholder="Message *"></textarea>
<input type="submit" id="submit" class="contact submit" value="Send message"/>
</form>
Person.java
public class Person {
private int id;
private String name;
private String lastName;
private String email;
private String subject;
private String message;
....
}
控制器
@Controller
public class ApplicationController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String indexPage() {
return "index";
}
@RequestMapping(value="/person", method=RequestMethod.GET)
public String contactForm(Model model) {
model.addAttribute("person", new Person());
return "index";
}
@RequestMapping(value="/person", method=RequestMethod.POST)
public String contactSubmit(@ModelAttribute Person person, Model model) {
model.addAttribute("person", person);
return "result";
}
}
我查看了Spring-boot and Thmeleaf setup,看起来我的设置是相同的。
--------- 更新 1 -----------
我已更改我的 post 方法以包含 BindingResult,但没有成功。
@RequestMapping(value="/person", method=RequestMethod.POST)
public String contactSubmit(@Valid @ModelAttribute Person person, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()){
System.out.println("There was a error "+bindingResult);
System.out.println("Person is: "+ person.getEmail());
return "index";
}
model.addAttribute("person", person);
return "result";
}
【问题讨论】:
标签: java spring-boot thymeleaf