During the recent project development process, I encountered the problem of converting the front-end js objects into java objects in the background many times. Record the method for later use.
To put it simply, use the JSON.stringify() method to convert js objects into js strings in the foreground, and receive json strings in the background and convert them into javaBean.
Frontend code:
Copy the code code as follows:
var data = {};
data.id = $('#id').val();
data.msg = $('#msg').val();
//Submit data
$.post(contextPath + '/XXX.do?'+new Date().getTime(),{data: JSON.stringify(data)},function(result){
alert(result);
});
Code behind:
Copy the code code as follows:
@RequestMapping("/XXX")
public void save(HttpservletResponse response,String data){
if(!StringUtils.isEmpty(data)){
//Convert json string to javaBean
Msg msg = (Msg) JSONObject.toBean(JSONObject.fromObject(data),Msg.class);
...
}
}