据我所知,这是正确设置但我收到以下错误:
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,看起来我的设置完全相同。
--------------------- Update 1 ----------------------- < / p>
我已将我的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";
}
答案 0 :(得分:5)
您忘记在<{1}}之后添加BindingResult
:
@ModelAttribute
我已经回答了这样的问题:
答案 1 :(得分:0)
首先我在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";
}
哪个是对的!
我不得不删除第一个方法并且它有效。
答案 2 :(得分:0)
在调用post方法之前,必须初始化Model属性(使用GET方法)。
在您的情况下,您需要在控制器中执行model.addAttribute("person",new Person());
的另一种方法,并且必须在发布前调用它。
请参考以下链接: https://spring.io/guides/gs/handling-form-submission/ 要么 http://forum.thymeleaf.org/Neither-BindingResult-nor-plain-target-object-for-bean-name-miniDoniie-available-as-request-attribute-td4027859.html
它在控制器中具有GetMapping
和PostMapping
。