Jsp页面传Json数据到服务端,转对象或集合进行数据处理

来源:互联网 发布:mac安装软件 编辑:程序博客网 时间:2024/06/08 07:37

需求:1、将页面数据带到服务端并转成对象,2、将页面的集合数据带到服务端转List
实现:用ajax请求传递数据,数据格式为json

JS方法:

testJsonMethod = function(){// 员工信息var employeeInfo = {emplNum : '123',emplName : 'lee',telNum : '18888888888'};// 标签信息var dataParam = [];for(var i=0; i<3; i++){var employeeLabel = {labelName : 'name' + i,labelOrder : i,labelRemarks : 'remark' + i}dataParam.push(employeeLabel);}var jsonEmployee = JSON.stringify(employeeInfo);var jsonLabelList = JSON.stringify(dataParam);// cache  Boolean (默认: true) 设置为 false 将不会从浏览器缓存中加载请求信息。// async  Boolean (默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行$.ajax({type : 'post',url : '${ctx}/admin/employee/testJsonMethod',data : {jsonEmployee:jsonEmployee, jsonLabelList:jsonLabelList},cache : false,dataType : 'json',success : function(data){alert(data);},error : function() {alert("异常!");}});}
服务端方法:

import net.sf.json.JSONArray;import net.sf.json.JSONObject;@RequestMapping(value = "/testJsonMethod", method = RequestMethod.POST)public ModelAndView testJsonMethod(String jsonEmployee, String jsonLabelList) throws Exception {SysEmployeeInfo sysEmployeeInfo = (SysEmployeeInfo) JSONObject.toBean(JSONObject.fromObject(jsonEmployee), SysEmployeeInfo.class);List<SystLabelInfo> systLabelInfoList = (List<SystLabelInfo>) JSONArray.toCollection(JSONArray.fromObject(jsonLabelList), SystLabelInfo.class);return new ModelAndView(JSON_VIEW).addObject(Constant.RETURN, true);}


阅读全文
0 0
原创粉丝点击