spring - JSP - AJAX and Controller gets the Object but does not update modelAttriibute -
when page loads, populate table.
@requestmapping(value = { "/access" }, method = requestmethod.get) public string access(modelmap model) { list<userdto> users = userservice.findallusers(); model.addattribute("users", users); userdto user = new userdto(); model.addattribute("user", user); return "access"; }
when user clicks edit on 1 row, ajax called.
function getdetails(id) { $.ajax({ type : "get", data : {id: id}, url : "get-details", cache : false, success : function(response) { // code here }, }); }
on controller, userdto id provided.
@requestmapping(value = { "/get-details" }, method = requestmethod.get) @responsebody public string getdetails(@requestparam("id") string id, modelmap model) { userdto user = userservice.findbyid(id); model.addattribute("user", user); return access; //'access' current page }
in jsp have...
<form:form method="post" modelattribute="user" class="form-horizontal">
my problem attributes of "user" remains null , not updated. note after calling ajax calls controller, userservice.findbyid has result. missing?
thanks.
update
i think need update modelattribute="user" in jsp using response after ajax call. because using "user" attribute , binding fields , errors
<form:form method="post" modelattribute="user" class="form-horizontal"> <div class="modal-content"> <div class="modal-body"> <!-- default panel contents --> <form:input type="hidden" path="id" id="id" /> <div class="row"> <div class="form-group col-md-14"> <label class="col-md-3 control-label" for="firstname">first name</label> <div class="col-md-7"> <form:input type="text" path="firstname" id="firstname" class="form-control input-sm" /> <div class="has-error"> <form:errors path="firstname" class="help-inline" /> </div> </div> </div> </div> </div> </div> </form:form>
if set directly input field, having error if form submitted error (e.g. empty fields)
severe: servlet.service() servlet [dispatcher] in context path [/sssbackend] threw exception [java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'user' available request attribute] root cause java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'user' available request attribute @ org.springframework.web.servlet.support.bindstatus.(bindstatus.java:144) @ org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.getbindstatus(abstractdataboundformelementtag.java:168) @ org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.getpropertypath(abstractdataboundformelementtag.java:188) @ org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.getname(abstractdataboundformelementtag.java:154) @ org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.writedefaultattributes(abstractdataboundformelementtag.java:117) @ org.springframework.web.servlet.tags.form.abstracthtmlelementtag.writedefaultattributes(abstracthtmlelementtag.java:422)
ok, want object in success: function(response){ }
should doing :
@requestmapping(value = { "/get-details" }, method = requestmethod.get) @responsebody public userdto getdetails(@requestparam("id") string id, modelmap model) { userdto user = userservice.findbyid(id); return user; //return object not view name }
and in ajax part :
success: function(response){ //response = userdto alert(response.name); //for example }
Comments
Post a Comment