spring - th:if="${#fields.hasErrors()}" causes Exception evaluating SpringEL expression -
i'm trying validation on 1 field of form 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>
the attribute th:if="${#fields.haserrors('name')}" causes 500 error me
my controller :
@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"; } }
my entity :
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(); } }
the console return following error:
java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'person1' available request attribute.
but think doesnt have because if remove p tag runs normally.
the problem make redirect, (you make "redirect:all"). because of redirect, not pass object person1 error "java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'person1' available request attribute".
if want post code of /all requestmapping have
probably want this
@requestmapping(value = "/edit", method = requestmethod.post) public string editperson(@valid @modelattribute(value="person1") personrequest person, bindingresult result) { if(result.haserrors()) { return "edit";//change name of html page want go } else { //probably here return in page persons personservice.update(person.getid(), person.getname()); return "redirect:all"; } }
Comments
Post a Comment