jquery - th:field causes org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor (500 error) -
i'm trying validation input fields on thymeleaf, , working fine until added in th:field validation
causes java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'person1' available request attribute
also, don't know causes entity :
@entity @table(name="person") public class person implements serializable{ @id @generatedvalue(strategy = generationtype.identity) @column(name = "id") private long id; @notnull(message = "cannot null") @column(name = "name") private string name; @onetomany(mappedby = "person") private list<phonenumber> phonenumber; public person() { super(); } public long getid() { return id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public list<phonenumber> getphonenumber() { return phonenumber; } public void setphonenumber(list<phonenumber> phonenumber) { this.phonenumber = phonenumber; } } my controller :
@controller public class personcontroller { @autowired personservice personservice; @requestmapping(value = "/all", method = requestmethod.get) public string getallperson(model model) { arraylist<person> listperson = personservice.getall(); model.addattribute("listperson",listperson); return "/all"; } @requestmapping(value = "/edit", method = requestmethod.post) public string editperson(@valid @modelattribute(value="person1") personrequest person, bindingresult result) { if(result.haserrors()) { return "/all"; } else { personservice.update(person.getid(), person.getname()); return "redirect:all"; } } } my form :
<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" th:field="*{name}" placeholder="name" id="edit-modal-person-name" name="name" value=""/> <p th:if="${#fields.haserrors('name')}" class="label label-danger" th:errors="*{name}">name error</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> <script> $(document).ready(function(){ $(".buttonedit").click(function(e){ e.preventdefault(); var id = $(this).attr('data-id'); $.ajax({ type: "get", url : "/getoneperson", data : 'id=' + id, datatype : "text", success : function(result){ $('#edit-modal-person-id').val(result.id); $('#edit-modal-person-name').val(result.name); $('#phonenumberedit').prop('th:attr','data-id='+id); }, error : function(){ alert("error"); } }); }); }); </script> is there anyway avoid using th:field? or alternative way validate field?
Comments
Post a Comment