EXTJS与后台通信问题
前台代码
var cm = new Ext.grid.ColumnModel( [ {
header : '编号',
dataIndex : 'id'
}, {
header : '用户名',
dataIndex : 'username',
width : 200
}, {
header : '角色',
dataIndex : 'role',
width : 200
} ]);
var data = [ [ '1', 'name1', 'desn1' ], [ '2', 'name1', 'desn1' ],
[ '3', 'name1', 'desn1' ], [ '4', 'name1', 'desn1' ],
[ '5', 'name1', 'desn1' ] ];
var create = Ext.data.Record.create([{
name : 'id',
mapping : 'id',
type : 'int'
},
{
name : 'username',
mapping : 'username',
type : 'string'
},
{
name : 'role',
mapping : 'role',
type : 'string'
}
]);
var ds=new Ext.data.Store({
remoteSort : true,
proxy:new Ext.data.HttpProxy({
url : 'userAction_getUsers.action',
method: 'POST'
}),
reader:new Ext.data.JsonReader({
root : 'users',
totalProperty : 'totalCount',
id : 'id'
},
// create
[{name : 'id', type : 'int'},
{name : 'username'},
{name : 'role'}]
),
successProperty: 'success'
});
var grid=new Ext.grid.GridPanel({
renderTo:Ext.getBody(),
store: ds,
width:800,
height:600,
cm:cm,
loadMask:{msg:'正在加载中,请稍后..'}
// bbar: new Ext.PagingToolbar({
// pageSize: 4,
// store: ds,
// displayInfo: true,
// beforePageText:"当前第",
// afterPageText:"页,共{0}页",
// lastText:"尾页",
// nextText :"下一页",
// prevText :"上一页",
// firstText :"首页",
// refreshText:"刷新页面",
// displayMsg: '显示第{0} 条到{1} 条记录,一共{2} 条',
// emptyMsg: "没有记录"
// }),
// tbar:[{text:"新增账号", handler:add_user},
// {text:"修改账号", handler:edit_user},
// {text:"删除账号", handler:del_user}]
});
grid.render();
ds.load({
// params:{start : 0,
// limit : 4}
// ,
callback:function(record,options,success)
{
if (success == false)
{
Ext.Msg.buttonText.ok = "确定";
Ext.Msg.alert("错误", "加载失败");
}
else
{
Ext.MessageBox.alert("提示", "success")
}
} });
后台的action方法
public String getUsers() throws IOException {
List<User> list = userService.findAll();//这里的方法说明同上
JSONObject json = new JSONObject();
json.put("success", true);
JSONArray jsonObjects = new JSONArray();
int i=0;
Iterator<User> it = list.iterator();
while(it.hasNext()){
JSONObject jsonObject = new JSONObject();
User user = it.next();//这里的Users对象就不多说了,同上
jsonObject.put("id",user.getId());
jsonObject.put("username", user.getUserName());
jsonObject.put("role", user.getRole());
jsonObjects.add(i++, jsonObject);
}
json.put("totalCount", list.size());
json.put("users", jsonObjects);
outputResult(json.toString());
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json; charset=GBK");
response.getWriter().write(json.toString());
response.getWriter().print(json.toString());
return null;
}
structs.xml的配置
<action name="userAction_getUsers" class="userAction" method="getUsers">
<result type="json">
<!-- <param name="root">users</param> -->
</result>
<result name="error">/login.jsp</result>
</action>
最后的结果在load的时候success返回 false,这个问题困扰我2天了,请各位大虾帮帮忙啊~~~