th:if =" $$ ## fields.hasErrors()}"导致异常评估SpringEL表达式 [英] th:if="${#fields.hasErrors()}" causes Exception evaluating SpringEL expression
问题描述
正如大家所看到的,我正在尝试对表单的1个字段进行验证:
I'm trying to do validation on 1 field of the form as you guys can see:
<div id ="EditModal" class="modal fade" role="dialog">
<form class="modal-dialog" th:action="@{/Edit}" th:object="${person1}" method="POST">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<input type="hidden" id="edit-modal-person-id" name="id" value=""/>
<input type="text" placeholder="Name" id="edit-modal-person-name" name="name"/>
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}" th:class="'error'">something</p>
<div>
<a class="btn btn-default pull-right" id="PhoneNumberEdit">Edit Phone Number</a>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="SubmitEdit" class="btn btn-default" >Submit</button>
<button type="button" class = "btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</form>
</div>
属性th:if="${#fields.hasErrors('name')}"
总是对我造成500错误.
the attribute th:if="${#fields.hasErrors('name')}"
always causes the 500 error for me.
我的控制器:
@RequestMapping(value = "/Edit", method = RequestMethod.POST)
public String editPerson(@Valid @ModelAttribute(value="person1") PersonRequest person, BindingResult result) {
if(result.hasErrors()) {
return "redirect:All";
}
else {
personService.update(person.getId(), person.getName());
return "redirect:All";
}
}
我的实体:
public class PersonRequest {
@NotNull
private long id;
@NotEmpty
@Name
private String 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;
}
public PersonRequest() {
super();
}
}
控制台返回以下错误:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'person1' available as request attribute.
但是我认为这与它无关,因为如果我删除p标签,它将正常运行.
But I think that doesnt have anything to do with this because if I remove p tag it runs normally.
推荐答案
问题是您进行了重定向,(您进行了"redirect:All"). 由于重定向,您不会传递对象person1,因此错误"java.lang.IllegalStateException:BeanResult'person1'的BindingResult或普通目标对象都不能用作请求属性".
The problem is that you make a redirect, (you make a "redirect:All"). Because of the redirect, you do not pass object person1 thus the error "java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'person1' available as request attribute".
如果您要发布/all requestMapping的代码
If you want post the code of the /all requestMapping that you have
可能您想要这样的东西
@RequestMapping(value = "/Edit", method = RequestMethod.POST)
public String editPerson(@Valid @ModelAttribute(value="person1") PersonRequest person, BindingResult result) {
if(result.hasErrors()) {
return "edit";//change it to the name of the html page that you want to go
}
else {
//probably here you return in the page with all the persons
personService.update(person.getId(), person.getName());
return "redirect:All";
}
}
这篇关于th:if =" $$ ## fields.hasErrors()}"导致异常评估SpringEL表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!