ext ajax json与struts实例
ext的javascript代码:
//提交修改 function submitUpdate(grid,colIndex,rowIndex,id){ //后台semanticsBean var sentence = Ext.getCmp('updateSentence').getValue(); var template = Ext.getCmp('updateTemplatesql').getValue(); var tableids = Ext.getCmp('updateTableids').getValue(); var jsonBean = { 'bean.id' : id, 'bean.sentence' : sentence, 'bean.template' : template, 'bean.tableids' : tableids } var bean = {id:id,sentence:sentence,template:template,tableids:tableids}; Ext.Ajax.request({ url:'./modifySemantics', params:jsonBean, success:function(r,o){ reRenderTable(grid,colIndex,rowIndex,bean); }, failure:function(){ Ext.Msg.alert("更新有误","更新有误,当前数据过旧,请刷新重试"); } }); }
struts配置文件:
<!-- 更新语义库记录 --> <action name="modifySemantics" class="NLManageAction" method="updateSemanticsList"> </action>
strut java代码:
类中属性
// 前台传输实体bean private SemanticsSqlTemp bean = new SemanticsSqlTemp();
public String updateSemanticsList(){ try{ if(this.bean == null || this.bean.getId() == 0){ logger.error("更新语义库sql模板报错,传输数据缺少ID"); return Action.NONE; } ArrayList<Object> keyvalueList = new ArrayList<Object>(); ArrayList<Class<?>> classtypeList = new ArrayList<Class<?>>(); bean.setUpdateData(keyvalueList, classtypeList); String sql = "update sql_template_set set sentence = ?,templatesql = ?,tableids = ? where id = ?"; NLSqlAnalysis.updateByID(sql, bean, keyvalueList, classtypeList); return Action.NONE; }catch (Exception e) { logger.error("更新语义库sql模板报错",e); return Action.ERROR; } }
这样就可以让struts接收json对象
再附上一个解析传回的json对象代码
function showSemanticsList(type,renderDivID) { //var showradio = Ext.getCmp("showRadioGroup"); //var showtype = getRadioInputValue(showradio);//获取数据标志 Ext.Ajax.request({ url:'./showSemantics', params:{ showType:type }, success : function(r,o){ var re = Ext.decode(r.responseText); if(re.length>0) showSemanticsData(type,re,renderDivID);//TODO: else{ document.getElementById(renderDivID).innerHTML = ""; Ext.Msg.alert("无所选数据"); } }, failure : function(){ Ext.Msg.alert("语义库查询有误","语义库查询有误"); } }); }
在js中解析json对象:
function showItem(grid,colIndex,rowIndex){ var id = rowIndex.data.id; Ext.Ajax.request({ url:'./getSemantics', params:{id:id}, success:function(r,o){ var re = Ext.decode(r.responseText); showItemEditPage(2,grid,colIndex,rowIndex,re); }, failure:function(){Ext.Msg.alert("语义查询有误","语义查询有误");} }); }