[英]ThymeLeaf: Neither BindingResult nor plain target object for bean name available as request attribute
[英]Thymeleaf Neither BindingResult nor plain target object for bean name 'person' available as request attribute
據我所知,這是正確設置的,但我收到以下錯誤:
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>
人.java
public class Person {
private int id;
private String name;
private String lastName;
private String email;
private String subject;
private String message;
....
}
Controller
@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 和 Thmeleaf 設置,看起來我的設置是相同的。
--------------------- 更新 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";
}
您忘記在@ModelAttribute
之后添加BindingResult
:
@RequestMapping(value="/person", method=RequestMethod.POST)
public String contactSubmit(@ModelAttribute Person person, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
//errors processing
}
model.addAttribute("person", person);
return "result";
}
我已經回答了這樣的問題:
Model 屬性必須在調用 post 方法之前初始化(使用 GET 方法)。 在您的情況下,您需要 controller 中的另一種方法,它執行model.addAttribute("person",new Person());
並且必須在發布前調用。
請參考以下鏈接: https://spring.io/guides/gs/handling-form-submission/或http://forum.ZDB38BD6F666CEF7A2F938F392EB71FDA-Z.intarget-object-forResult-bean-nor-pla -miniDoniie-available-as-request-attribute-td4027859.html
它在 controller 中具有GetMapping
和PostMapping
。
您需要為“person”聲明一個@ModelAttribute。 就像是:
@ModelAttribute("Myperson")
public Person newPerson() {
return new Person();
}
然后,聲明你@PostMapping 並在@ModelAttribute 中設置名稱“person”。
@RequestMapping(value="/person", method=RequestMethod.POST)
public String contactSubmit(@ModelAttribute Person person, Model model) {
model.addAttribute("person", person);
return "result";
}
我希望它適用於閱讀本文的人:)
首先我有index.html
中的表格
@RequestMapping(value = "/", method = RequestMethod.GET)
public String indexPage(){
return "index";
}
所以當我的表格:
<form th:action="@{/person}" th:object="${person}" method="post" >
<input type="text" th:field="*{subject}" class="contact col-md-6" placeholder="Subject *" />
<input type="text" th:field="*{name}" class="contact col-md-6" placeholder="Name *" />
<input type="text" th:field="*{lastName}" class="contact col-md-6" placeholder="Last Name *" />
<input type="email" th:field="*{email}" class="contact noMarr col-md-6" placeholder="E-mail address *" />
<textarea name="comment" class="contact col-md-12" th:field="*{message}" placeholder="Message *" ></textarea>
<input type="submit" id="submit" class="contact submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
正在尋找/
它正在使用上述方法,而不是:
@RequestMapping(value="/", method=RequestMethod.GET)
public String contactForm(@Valid @ModelAttribute("person") Person person, BindingResult bindingResult,
HttpServletRequest request, Model model) throws IOException {
if(bindingResult.hasErrors()){
System.out.println("There was a error "+bindingResult);
return "index";
}
model.addAttribute("person", new Person());
return "index";
}
哪個是正確的
我不得不刪除第一種方法並且它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.