JsonConfig过滤对象属性
转载自:http://blog.csdn.net/java20100406/article/details/6232055
通过配置JsonConfig过滤有可能引起循环的字段,如果不这样,根据hibernate的原则会不断的级联查询.
*************************************Action******************************************************
1.对象。
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes( new String[]{ "creator", "module", "testPoints","testProcesses","highLevelRequirements","lowLevelRequirements"} );
String result = JSONObject.fromObject( testCase对象 ,jsonConfig).toString();
2.List数组
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes( new String[]{ "creator", "module", "testPoints","testProcesses","highLevelRequirements","lowLevelRequirements"} );
JSONArray lineitemArray = JSONArray.fromObject( testCases集合List ,jsonConfig);
String result = JSONArray.fromObject(lineitemArray).toString();
3.Map对象
Map map = new HashMap();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes( new String[]{ "creator", "module", "testPoints","testProcesses","highLevelRequirements","lowLevelRequirements"} );
JSONArray lineitemArray = JSONArray.fromObject( testCases集合List ,jsonConfig);
map.put("testCases", lineitemArray);
String result = JSONObject.fromObject( map对象 ).toString();
************************************struts.xml*******************************************************
<package name="ajax" extends="json-default" namespace="/ajax">
..........
<action name="childrenAjax" class="**********.AssociationBaseAction" method="getChildren">
<result name="success" type="json">
<param name="root">result</param>
</result>
</action>
..........
</package>
***************************************javascript****************************************************
1.对象。
$.ajax({
type: "POST",
url: "ajax/oneAjax.html",
dataType: "json",
data: "targetId=" + val ,
success: function(result){
var json=eval("("+ result + ")");
$('#selectedResult').append( "<div id=" + json.id +"><input type='checkbox' name='selected' value='" + json.id + "'>"+ json.id + " : " + json.name +"</div>" );
}
});
2.List数组
$.ajax({
type: "POST",
url: "ajax/childrenAjax.html",
dataType: "json",
data: url,
error: function(){
alert("<fmt:message key='errors.msg'/>");
},
success: function(result){
var org1=eval("("+ result + ")");
for(var i=0;i<org1.length;i++){
menuTree.add(org1[i].id,ID,org1[i].id + " : " + org1[i].name);
}
}
});
3.Map对象
$.ajax({
type: "POST",
url: "ajax/associationAjax.html",
dataType: "json",
data: "associationObjectIds=" + idString () ,
success: function(result){
var json=eval("("+ result + ")");
var dataItems = json.dataItems;
var testCases = json.testCases;
var sysReqs = json.sysReqs;
var hlrReqs = json.hlrReqs;
var llrReqs = json.llrReqs;
var funcInfos = json.funcInfos;
.....................................................
if (testCases.length != 0){
$('#testCaseDiv').empty();
var url = "";
for(var i =0;i<testCases.length;i++){
url = url + "<div id='testCaseDiv" + i + "' style='height:30px;overflow:hidden;'>";
url = url + "<table width=100% border=1 cellspacing=0 cellpadding=1 bordercolor=ffffff bordercolordark=#ffffff bordercolorlight=B9B9B9>";
url = url + "<tr align=center>";
url = url + "<td width=20% height=28 bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.Id'/></td>";
url = url + "<td width=30% height=28 align=left>";
url = url + testCases[i].id + " ";
url = url + "</td>";
url = url + "<td width=20% height=28 bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.creator'/></td>";
url = url + "<td width=30% height=28 align=left >";
url = url + "<span style='float:left' id='testCaseSpan" + i + ".creator'>" + testCases[i].creatorName + "</span>";
url = url + "<span style='float:right' id='testCaseSpan" + i + "' onclick=shoppingcat('testCaseDiv" + i + "','testCaseSpan" + i + "') title=open><img src='./images/down.png'/></span>";
url = url + " ";
url = url + "</td>";
url = url + "</tr>";
url = url + "<tr align=center>";
url = url + "<td width=20% height=28 bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.name'/></td>";
url = url + "<td colspan=3 height=28 align=left><span id='testCaseSpan" + i + ".name'>" + testCases[i].name + "</span> </td>";
url = url + "</tr>";
url = url + "</tr>";
url = url + "<tr align=center>";
url = url + "<td width=20% height=60 bgcolor=#EFEFEF><fmt:message key='summary.html.testCase.description'/></td>";
url = url + "<td width=80% height=60 colspan=3 align=left ><span id='testCaseSpan" + i + ".description'>" + testCases[i].description + "</span>";
url = url + "</td>";
url = url + "</tr>";
url = url + "</table>";
url = url + "</div>";
}
$('#testCaseDiv').append(url);
}
...........................................
}
});