在发布数据之前的剑道中,我可以看到如下的值,
{"models":[{"id":1,"publisherName":"Srini","active":false}]}
您可以在控制器中看到完成Map集合的映射。如何将数据从httprequest映射到模型类Publisher?
@RequestMapping(value = "/admin/updatePublisher.htm", method = RequestMethod.POST, produces = "application/json")
public void updatePublisher(HttpServletRequest request,HttpServletResponse response) throws Exception {
Map<String, Object> data = Collections.emptyMap();
if (0 < request.getContentLength()) {
data = new ObjectMapper().readValue(request.getInputStream(), Map.class);
}
logger.debug("updatePublisher data: "+data); // output : {models=[{id=1, publisherName=Srinisdasadsa, active=false}]}
response.setStatus(HttpServletResponse.SC_OK);
}
public class Publisher {
int id;
String publisherName;
boolean active;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPublisherName() {
return publisherName;
}
public void setPublisherName(String publisherName) {
this.publisherName = publisherName;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
答案 0 :(得分:0)
以下是Kendo UI demo正在做的事情:
@RequestMapping(value = "/editing/update", method = RequestMethod.POST)
public @ResponseBody List<Product> update(@RequestBody ArrayList<Map<String, Object>> models)
{
List<Product> products = new ArrayList<Product>();
for (Map<String, Object> model : models) {
Product product = new Product();
product.setProductId((int)model.get("productId"));
product.setProductName((String)model.get("productName"));
product.setUnitPrice(Double.parseDouble(model.get("unitPrice").toString()));
product.setUnitsInStock((int)model.get("unitsInStock"));
product.setDiscontinued((boolean)model.get("discontinued"));
product.setCategoryId((int)model.get("categoryId"));
products.add(product);
}
product.saveOrUpdate(products);
return products;
}