The problem of foreground js object transforming Java object in the background is discussed

  • 2020-03-30 00:59:29
  • OfStack

In the process of recent project development, I encountered the problem of js object being converted into Java object in the background for many times. Record the procedure for use.

Simply put, the json.stringify () method is used in the foreground to convert the js object to a js string, and the background accepts the JSON string and converts it to a javaBean.

Foreground code:
 
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); 
}); 

Background code:
 
@RequestMapping("/XXX") 
public void save(HttpservletResponse response,String data){ 
if(!StringUtils.isEmpty(data)){ 

//Json strings are converted to javabeans
Msg msg = (Msg) JSONObject.toBean(JSONObject.fromObject(data),Msg.class) ;  

...... 
} 
} 

Related articles: