When the page loads, it populate the 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 the user clicks Edit on one row, ajax will be called.
function getDetails(id) {
$.ajax({
type : "get",
data : {id: id},
url : "get-details",
cache : false,
success : function(response) {
// code here
},
});
}
On the controller, it will get the UserDTO with the 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' is the current page
}
In my jsp I have...
<form:form method="POST" modelAttribute="user" class="form-horizontal">
My problem is that the attributes of "user" remains null and not updated. Note that after calling ajax that calls controller, userService.findById has result. What am I missing?
Thanks.
UPDATE
I think I really need to update modelAttribute="user" in my jsp using the response after ajax call. Because I am using the "user" attribute and binding it to fields and 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 I will set directly the input field, I am having an error if the form is submitted with error (e.g. empty fields)
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/SSSBackEnd] threw exception [java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute] with root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:144) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:117) at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
What I did is to set model attribute in controller and let it return the jsp name itself (access). (no changes made)
Then on Ajax response, I added an update to access html using this code.
$('#accessid').html(response);