Ajax调用struts2的参数构造

把javascript的对象,转换为struts2 action的调用参数格式。以下为源码:


$.strutsParam = function(actionPropertyName, obj) {

var newObj = {};

strutsParam(actionPropertyName, obj, newObj);

return newObj;


//参数转换,递归函数

function strutsParam(actionPropertyName, obj, newObj) {

if (Array.isArray(obj)) {

var arr = obj;

for ( var i = 0; i < arr.length; i++) {

var arrItem = arr[i];

if (typeof (arrItem) == "object") {

//对象有属性

for ( var p in arrItem) {

var key = actionPropertyName + "[" + i + "]." + p;

if (Array.isArray(arrItem[p])) {

//对象为数组,递归

strutsParam(key, arrItem[p], newObj);

} else {

//console.log(typeof (arrItem[p]));

if (typeof (arrItem[p]) != "function") {

newObj[key] = arrItem[p];

}

}

}

} else {

//对象无属性

var key = actionPropertyName + "[" + i + "]";

//console.log(typeof (arrItem));

if (typeof (arrItem) != "function") {

newObj[key] = arrItem;

}

}


}

} else {


if (typeof (obj) == "object") {

for ( var p in obj) {

//console.log(typeof (obj[p]));

var key = actionPropertyName + "." + p;

if (typeof (obj[p]) != "function") {

newObj[key] = obj[p];

}

}

} else {

//对象无属性

var key = actionPropertyName;

//console.log(typeof (obj));

if (typeof (obj) != "function") {

newObj[key] = obj;

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11287.html