extjs 与java程序数据交互
- 提问者网友:欲望失宠
- 2021-04-24 17:17
- 五星知识达人网友:等灯
- 2021-04-24 18:36
- 1楼网友:往事埋风中
- 2021-04-24 22:06
- 2楼网友:末日狂欢
- 2021-04-24 21:09
json是作为js和java交互的工具。
例如前台extjs代码:
grid = new Ext.grid.GridPanel({ id:'SearchGrid', height:400, loadMask:{msg:"数据加载中,请稍等..."}, store:ds, columns:[ {header:'部门编号',dataIndex:'deptid',width:90}, {header:'部门名称',dataIndex:'deptname',width:120}, {header:'部门人数',dataIndex:'deptperson',width:120}, {header:'已下井人数',dataIndex:'inwell',width:120}, {header:'未下井人数',dataIndex:'noneinwell',width:120}, {header:'备注',dataIndex:'memo',width:180} ], tbar:['时间:','-',{xtype:'datefield', maxValue:new Date().format("Y-m-d"), minText:'选择时间', format:"Y-m-d", value:new Date().format("Y-m-d"), id:'time' },'-',{ text:'查询', iconCls:'tool-find', handler:function (){ document.all('query').value = Ext.getDom('time').value; //alert(document.all('query').value); searchFn(Ext.getDom('time').value); ds.reload(); } },'-', { text:'导出到excel', iconCls:'prsss-button', handler:function (){ var myParams = { cc :document.getElementById('query').value }; Ext.Ajax.request({ params:myParams, method:'post', url : '/KJ222/PersonInWell/toExcel', success: function () { window.open('/KJ222/excel/toExcel.jsp?thisname=PersonInWell'); },failure: function (){ Ext.MessageBox.alert('导出失败', '可能由于某种原因导致您的导出失败 '); } }); } },'-' ], border:false });
数据源:
var ds = new Ext.data.JsonStore({ url:'/KJ222/PersonInWell/getAll', fields:[ 'deptid','deptname','deptperson','inwell','noneinwell','memo' ], baseParams:{ limit:20 }, totalProperty:'total', root:'demo' }); ds.load({ params:{start:0} });
这个就是extjs的前台。当表格加载的时候会调用ds,ds的url指向的是后台,可以是servlet。
之后你在后台得要生成和json格式一模一样的数据。比如
{ "total":239, "rows":[ {"code":"001","name":"Name 1","addr":"Address 1","col4":"col4 data"}, {"code":"002","name":"Name 2","addr":"Address 2","col4":"col4 data"}, {"code":"003","name":"Name 3","addr":"Address 3","col4":"col4 data"}, {"code":"004","name":"Name 4","addr":"Address 4","col4":"col4 data"}, {"code":"005","name":"Name 5","addr":"Address 5","col4":"col4 data"}, {"code":"006","name":"Name 6","addr":"Address 6","col4":"col4 data"}, {"code":"007","name":"Name 7","addr":"Address 7","col4":"col4 data"}, {"code":"008","name":"Name 8","addr":"Address 8","col4":"col4 data"}, {"code":"009","name":"Name 9","addr":"Address 9","col4":"col4 data"}, {"code":"010","name":"Name 10","addr":"Address 10","col4":"col4 data"} ] }
(out.print(xxx))
你的控制器最后要打出来这样格式的数据。每列要和header里面想对应,才能赋值到表格里。
- 3楼网友:雪起风沙痕
- 2021-04-24 20:00
ExtJS 是一个Ajax框架,交互方式就是Ajax,而JSON是一种数据格式,只不过在ExtJS中,各种组件能够很好的识别JSON,根据header就能自动把数据填充到相应的地方。当然作为Ajax,你还可以提交其他类型的数据,不过一般都是字符串之类的。特殊一点的就是还可以提交file对象。
ExtJS和JAVA交互常见的有这么几个东西:
Ext.data.Store Ext.Ajax.request
举个例子:
Ext.Ajax.request({
url:'login.jsp', //与后台JAVA交互的地址,如果用了框架,这里可以换成action访问地址
success:function(){
},
failure:function(){
},
params:{
username:'Test', //这个就是我要提交的参数,这个并不是一个json
password:'123456',
json:'{username:'Test',password:'123456'}' // 这个是一个json,值包括了用户名和密码
}
})
后台:
获取用户名 String username=request.getParameter('username');
获取密码 String passoword=request.getParameter('password');
获取json JSONObject Json=JSONObject.fromObject(request.getParameter('json'));
- 4楼网友:北城痞子
- 2021-04-24 19:11
建议去下载有关EXt初级入门的书 然后再看看ext ajax 和json 然后再去做你想要做的