一:jsp中 //更新时获得数据信息 var url = "%=request.getContextPath()%/BudgetMaintainAction.do?cmd=getUpdateBudgetMaintainBeanbudgetinfoId="+budgetinfoId+"contractinfo_id_update="+contractinfo_id_update+"rand="+Math.random(); $.a
一:jsp中
//更新时获得数据信息
var url = "<%=request.getContextPath()%>/BudgetMaintainAction.do?cmd=getUpdateBudgetMaintainBean&budgetinfoId="+budgetinfoId+"&contractinfo_id_update="+contractinfo_id_update+"&rand="+Math.random();
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
timeout: 100000,
async: false,
error: function(){
alert('获得数据失败!');
},
success: function(jsonText){
//合同信息回写
jsonArrayWriteToForm(jsonText);//回写预算中的信息
}
});
}
//核心方法:form为form对象(暂时只支持1个)
//json回写到form
function jsonArrayWriteToForm(jsonText){
if(!jsonText){
return;
}
//获得对象精确类型
this.__getClass = function(object){
return Object.prototype.toString.call(object).match(/^\\[object\\s(.*)\\]$/)[1];
};
$.each(jsonText,function(i,item){
for ( var p in item ){
//构建回写的form字段
//alert(p+"--"+item[p]);
var objVal = item[p];
var valType = __getClass(objVal);
//数组集合
if(valType == 'Array'){
$.each(objVal,function(j,arrayItem){
for ( var jp in arrayItem ){
alert("array--"+jp+"--"+arrayItem[jp]);
}
});
}else{
//普通
if(form.elements[p]){
var typeInfo = __getClass(form.elements[p].length);//Number时有多个对象
if(typeInfo == 'Number' && form.elements[p].length != 1){
//可能重名或checkbox
for(var i =0,j=form.elements[p].length ; i<j ; i++){
if(form.elements[p][i].value != undefined) { //如果有value属性,暂只支持文本域
form.elements[p][i].value = objVal;
}
//是否checkbox等
}
}else{
if(form.elements[p].value != undefined) { //如果有value属性,暂只支持文本域
form.elements[p].value = objVal;
}
}
}
}
}
});
}
二:java后台
String jsonString = TooolUtil.getJsonString(beans);//beans 为Map<String,Object>类型的对象
TooolUtil.setTextAjax(jsonString, response);//jsonString为转换后的json数组集合
格式如:[{"id":"1212","name":"name1"},{"id":"22","name":"n2"}]
/**
* ajax返回html,包括json形式
*
* @param responseContent
*/
public static void setTextAjax(String responseContent,IResponse response) {
try{ //更新规则记录状态
response.getServletResponse().setCharacterEncoding("GBK");
//返回参数值到页面
PrintWriter out = response.getServletResponse().getWriter();
out.write(responseContent);
out.flush();
out.close();
}catch (Exception e) {
if(e instanceof BSException)throw (BSException)e;
throw new BSException("Ajax得到服务数据出错");
}
}
}
