Thymeleaf Bean 名称“person”的 BindingResult 和普通目标对象都不能用作请求属性
Posted
技术标签:
【中文标题】Thymeleaf Bean 名称“person”的 BindingResult 和普通目标对象都不能用作请求属性【英文标题】:Thymeleaf Neither BindingResult nor plain target object for bean name 'person' available as request attribute 【发布时间】: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";
【问题讨论】:
【参考方案1】:您忘记在您的@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";
我已经回答了这样的问题:
html form validation using thymeleaf not working spring boot【讨论】:
【参考方案2】:在调用 post 方法之前,必须初始化模型属性(使用 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
。
【讨论】:
【参考方案3】:首先我有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";
这是正确的!
我不得不删除第一个方法,它奏效了。
【讨论】:
以上是关于Thymeleaf Bean 名称“person”的 BindingResult 和普通目标对象都不能用作请求属性的主要内容,如果未能解决你的问题,请参考以下文章
Spring boot 如何将 Thymeleaf 视图解析器放入其 bean 容器中
Thymeleaf 内联 javascript spring bean 使用文本模板模式检查 null
Spring Boot / Thymeleaf / Hibernate:带有 Java 注解的 Sessionfactory Bean