前后台交互积累(一)

时间:2021-02-11 17:25:36

刚开始实习接触商业项目,想积累点开发经验,欢迎互相交流

一、前台通过JS中的方法去后台拿到数据,封装到map中,存入缓存,然后再通过方法,调用数据

前台JSP中部分代码:

var businessstatusMap= returndataMap(sy.contextPath + '/dicType/getDicTreeMap/PDW_OBJ_ENT_TradeStatus');
var economicTypeMap=returndataMap(sy.contextPath + '/dicType/getDicTreeMap/PDW_BASIC_ECONOMICTYPE');

function search(){
$('#gridList').datagrid({
url: sy.contextPath + '/enterprise/findJoinTrade',
method:'post',
queryParams: $("#searchForm").serializeJson(),
pagination: true,
striped: true,
nowrap:false,
singleSelect: false,
fit : true,
collapsible : true,
loading:true,
loadMsg:loadMsgs,
fitColumns:true,
showHeader:true,
pageNumber:1,
pageSize:pageSizes,
pageList:pageLists,
rownumbers:true,
idField:'id',
sortName:'id',
sortOrder:'desc',
columns:[[
{field:'cnname', title:'业户名称', align:'left', sortable:'true', width:'25%'},
{field:'abbreviation', title:'业户简称', align:'left', sortable:'true', width:'10%'},
{field:'businessstatus', title:'业户经营状态', align:'left', sortable:'true', width:'10%',
formatter: function(val){
return businessstatusMap[val];
}},
{field:'registeredaddress', title:'营业执照登记地', align:'left', sortable:'true', width:'15%'},
{field:'economictype', title:'经济类型', align:'left', sortable:'true', width:'15%',
formatter: function(val){
return economicTypeMap[val];
}},
{field:'enterprisetrade', title:'管理机构', align:'left', sortable:'true', width:'15%',
formatter: function(value){
if (value){
return value.manageragence;
} else {
return '';
}
}
},

{field:'id', title:'操作', align:'left', width:'8%',
formatter: function(value, row, index){
var v = '';
v += viewBtn(row.id);
return v;
}
}
]],
onLoadError: function(){
$.messager.alert(warningTitle, errorMsgs, 'warning');
closeProgress();
},onBeforeLoad : function(param) {
startProgress();
},
onLoadSuccess : function(data) {
clearGrid($('#gridList'));
closeProgress();
}
});
}

JS代码:

//添加解析json字符串方法
function returndataMap(url){
var map ={};
$.get(url, null, function(data) {
$.each(data, function(i, val) {
//map[val.code] = val.text;
$.each(val, function(key,value){
map[key] = value;
});
});
});
return map;
}

后台部分代码:

protected void renderJson(Map map, HttpServletResponse response) {
try {
response.setContentType("application/json; charset=utf-8");
ObjectMapper objectMapper = new ObjectMapper();
JsonGenerator jsonGenerator = objectMapper.getJsonFactory()
.createJsonGenerator(response.getWriter());
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("rows");
jsonGenerator.writeObject(map);
jsonGenerator.writeEndObject();
if (jsonGenerator != null) {
jsonGenerator.flush();
}
if (!jsonGenerator.isClosed())
jsonGenerator.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

传入后台JSON格式:

{"rows":{"202":"过期(非在册)","203":"注销","204":"撤销/许可撤销","205":"吊销","301":"其他","201":"审批中/预增加","206":"歇业","2":"非在册","1":"在册","107":"整改","10301":"停业/暂停经营","106":"过期","10303":"停业整顿","104":"待注销","10302":"从未经营","103":"停业","102":"待转入","101":"正常/在册/运营"}}