JSON是轻量的数据交换格式,不限与语言, JSON格式:{KEY:Value,KEY1.Value1},类似于HashMap! Struts2与JQuery,都加入了对 JSON更好的支持.
最近在项目中遇到需要使用AjAx,请求Acton数据,而在Action的数据是以对象存放于集合中,要么使用使用拼串,返回数据,或者使用JSON返 回数据.
Struts2 xml 配置文件,还可以对不想要的数据过滤
<!--不对继承关系进行数据转换-->
false
<!-- 请求的action中的对象转换为json格式 当然在response中 -->
productList对action中的数据进行转换的对象中,如果不想对转换的数据进行JSON转换,可以在Struts2的配置文件中过滤,如果不对数据过滤,可能在应用启动时抛出异常,如果不进行过滤可以赋初值,也可以使用VO进行属性封装.
com.googlecode.jsonplugin.JSONException: java.lang.reflect.InvocationTargetException, 62
javax.servlet.ServletException: com.googlecode.jsonplugin.JSONException:
java.lang.reflect.InvocationTargetException
前台取JSON数据
function ajaxJson(hid){ var id=$("#productID"+hid).val(); var str= /^\d+$/ if(id=='' || !str.test(id)) { alert('ID不正确'); } var tableNum = $("#tableNum").val(); $.ajax({ type:"post", url:"admin_poOrder_getCommodityDetail.action", data:{productID:id}, dataType:"json", success:function(msg){ if(msg.length>0){ $("#pdname"+hid).html("<a href="dpDetail_listDetail.action?productId="+msg[0].id+"" target="_blank">"+msg[0].pdName+"</a>" +"<input id="subPdname"+hid+" />" ); $("#dispaly"+hid).html(msg[0].dpDisplay); $("#dispBrandName"+hid).html(msg[0].brandName); $("#dispBpName"+hid).html(msg[0].bpName); //hidden $("#subPdname"+hid).val(msg[0].pdName); $("#brandName"+hid).val(msg[0].brandName); $("#brandId"+hid).val(msg[0].brandId); $("#bpName"+hid).val(msg[0].bpName); $("#vendorId"+hid).val(msg[0].vendorId); $("#opderOption"+hid).val(msg[0].pdDetailName); $("#orderOptionCn"+hid).val(msg[0].pdDetailNameCn); var detailName="" for(var i=0;i<msg.length;i++){ detailName+="<input type=" name="poOrderLists["+hid+"].productName" type="hidden" value="+msg[i].pdId+" />"+msg[i].pdDetailNameCn+" "; } $("#detail"+hid).html(detailName); }else{ alert("没有商品"); } } }); }
Leave a Reply