nojave 2009-08-12 12:56
浏览 254
已采纳

GridPanel得不到Json数据,

思路
getuser.jsp -> submit -> redirectuserlist.action -> userlist.jsp -> searchuser.action -> userlist.jsp(GridPanel显示数据)

Struts.xml 相关
[code="xml"]
method="redirectUserList">
/userlist.jsp

method="searchUser">
/userlist.jsp

[/code]

相关action的代码
[code="java"]
public String searchUser() {

    try {
        ActionContext ctx = ActionContext.getContext(); 
        HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);        
        response.reset(); 
        response.setCharacterEncoding("utf-8"); 
        ComUserInfo comuserinfo = new ComUserInfo();
        if (comuserinfo.getUserid() != "") {
            List result = uo.searchUser(getUserid());
            JsonUtil ju = new JsonUtil();
            String jsresult = ju.list2json(result);
            this.userlist = ("{\"totalCount\": 3," + "\"userlist:\" " + jsresult + " }");
            System.out.println(userlist);
            response.getWriter().write(userlist);
            //response.getWriter().println(userlist);
            return SUCCESS;
        } else {
            ssn.add("errmsg", "User not login yet!");
            return ERROR;
        }
    } catch (Exception e) {
        ssn.add("errmsg", e.getMessage());
        return ERROR;
    }
}

public String redirectUserList(){
    return SUCCESS;
}

[/code]

userlist.jsp上的 js 代码:

[code="java"]

Ext.onReady(function() {
Ext.QuickTips.init();

// turn on validation errors beside the field globally
// Ext.form.Field.prototype.msgTarget = 'side';
var cm = new Ext.grid.ColumnModel([{
    header : '用户ID',
    dataIndex : 'userId'
}, {
    header : '用户昵称',
    dataIndex : 'usrName'
}, {
    header : '密码',
    dataIndex : 'usrPwd'
}, {
    header : '电子邮件地址',
    dataIndex : 'usrEmail'
}, {
    header : '密码提示问题',
    dataIndex : 'usrPwdQues'
}, {
    header : '密码提示问题答案',
    dataIndex : 'usrPwdAns'
}]);

var reader = new Ext.data.JsonReader({
    root : 'userlist',
    totalProperty: 'totalCount'

}, [{
    name : 'usrId',
    mapping : 'userId'
}, {
    name : 'usrName',
    mapping : 'usrName'
}, {
    name : 'usrPwd',
    mapping : 'usrPwd'
}, {
    name : 'usrEmail',
    mapping : 'usrEmail'
}, {
    name : 'usrPwdQues',
    mapping : 'usrPwdQues'
}, {
    name : 'usrPwdAns',
    mapping : 'usrPwdAns'
}]);

var ds = new Ext.data.Store({
    proxy : new Ext.data.HttpProxy({
        url : 'searchuser.action',
        method : 'POST'
        //method : 'GET'
    }),
    reader : reader,
    autoload : true
}); 

// ds.load();
// alert(ds.getCount()) ;

//var sm = new Ext.grid.CheckboxSelectionModel();

var grid = new Ext.grid.GridPanel({
    store : ds,
//  sm : sm,
    cm : cm,
    loadMask : {
        msg : ' 数据加载中...'
    },
    autoHeight : true

});

var vp = new Ext.Viewport({
    layout : "border",
    frame : true,
    title : "用户列表",
    autoHeight : true,
    items : [{// Viewport
        region : "north",
        height : 100,
        title : "顶部面板"
    }, {
        region : "south",
        height : 50,
        title : "底部面板"
    }, {
        region : "west",
        width : "20%",
        title : "左边面板"
    }, {
        region : "east",
        width : "20%",
        title : "右边面板"
    }, {
        region : "center",
        title : "中央面板",
        items : [frm = new Ext.FormPanel({
            items : [grid]
        }       /* Eof formpanel */)
        /* Eof border center */]
    }       /* Eof Viewport */]
});
grid.render();

vp.render(document.body);

});
[/code]

action返回的数据如下:

{"totalCount": 3,"userlist:" [
{"usrCreateBy":"","usrCreateDate":"","usrDisableDate":"",
"usrEmail":"Frankie@together.com","usrId":"80319474","usrName":"Frankie","usrPwd":"1233","usrPwdAns":"1+1","usrPwdQues":"2",
"usrRemark":"","usrUpdateBy":"","usrUpdateDate":""
},
{"usrCreateBy":"","usrCreateDate":"","usrDisableDate":"",
"usrEmail":"Justin@together.com","usrId":"80334272","usrName":"Justin","usrPwd":"1234","usrPwdAns":"2","usrPwdQues":"1+1",
"usrRemark":"nothing","usrUpdateBy":"","usrUpdateDate":""
},
{"usrCreateBy":"","usrCreateDate":"","usrDisableDate":"",
"usrEmail":"Justin@together.com","usrId":"sa","usrName":"sysadmin","usrPwd":"a","usrPwdAns":"2","usrPwdQues":"1+1",
"usrRemark":"nothing","usrUpdateBy":"","usrUpdateDate":""
}
]}

困扰好几天了,请大家帮忙看看了,谢谢了!
[b]问题补充:[/b]
感谢atian25 的回答!

利用你的方法,我已经可以确定json是没有问题了.利用var json={......}; ds.loaddata(json);我可以在GridPanel里看到数据.
但是还是没办法获得searchuser.action返回的数据.我想问题可能出在public String searchUser(){...} 和
var ds = new Ext.data.Store({

proxy : new Ext.data.HttpProxy({

url : 'searchuser.action',

method : 'POST'

//method : 'GET'

}),

reader : reader,

autoload : true

});

之间.我想请问,我的searchUser()方法和ds是否有问题?比如searchUser()是否一定要用response.getWriter().println(userlist);将数据打印出来,或者ds里的 method : 'POST' or method : 'GET' 会影响?

[b]问题补充:[/b]
[code="java"][/code][code="java"]
public String searchUser() {

try {

ActionContext ctx = ActionContext.getContext();

HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);

HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);

response.reset();

response.setCharacterEncoding("utf-8");

ComUserInfo comuserinfo = new ComUserInfo();

if (comuserinfo.getUserid() != "") {

List result = uo.searchUser(getUserid());

JsonUtil ju = new JsonUtil();

String jsresult = ju.list2json(result);

this.userlist = ("{\"totalCount\": 3," + "\"userlist:\" " + jsresult + " }");

System.out.println(userlist);

response.getWriter().write(userlist);

//response.getWriter().println(userlist);

return SUCCESS;

} else {

ssn.add("errmsg", "User not login yet!");

return ERROR;

}

} catch (Exception e) {

ssn.add("errmsg", e.getMessage());

return ERROR;

}

}

public String redirectUserList(){   
    return SUCCESS;   
}  

[/code]
打印出来的数据是:
{"totalcount":1,"userlist": [{"usrCreateBy":"","usrCreateDate":"","usrDisableDate":"","usrEmail":"Justin@together.com","usrId":"sa","usrName":"sysadmin","usrPwd":"a","usrPwdAns":"2","usrPwdQues":"1+1","usrRemark":"nothing","usrUpdateBy":"","usrUpdateDate":""}] }
我将此数据放到js中:

[code="java"]
var json = {
"totalcount" : 1,
"userlist" : [{
"usrCreateBy" : "",
"usrCreateDate" : "",
"usrDisableDate" : "",
"usrEmail" : "Justin@together.com",
"usrId" : "sa",
"usrName" : "sysadmin",
"usrPwd" : "a",
"usrPwdAns" : "2",
"usrPwdQues" : "1+1",
"usrRemark" : "nothing",
"usrUpdateBy" : "",
"usrUpdateDate" : ""
}]
}

// ds.load();
ds.loadData(json);
[/code]
可以成功显示,但是用ds.load();去获得action返回的时候却失败了.实在搞不明白.

[b]问题补充:[/b]
公司没办法装东西,看了下别人的,把代码简化了下,直接给页面打印字符串:
[code="java"]
public String searchUser() {
try {
ComUserInfo comuserinfo = new ComUserInfo();
if (comuserinfo.getUserid() != "") {
List result = uo.searchUser(getUserid());
String totalcount = tc.intToString(result.size());
JsonUtil ju = new JsonUtil();
String jsresult = ju.list2json(result);
setUserlist("{\"totalcount\" : \"1\",\"userlist\" : [{"
+ "\"usrId\" : \"sa\","
+ "\"usrName\" : \"sysadmin\"}]" + "}");
System.out.println(userlist);
return SUCCESS;
} else {
ssn.add("errmsg", "User not login yet!");
return ERROR;
}
} catch (Exception e) {
ssn.add("errmsg", e.getMessage());
return ERROR;
}
}
[/code]

js代码:
[code="java"]
/*

Ext.onReady(function() {
Ext.QuickTips.init();

// turn on validation errors beside the field globally
// Ext.form.Field.prototype.msgTarget = 'side';
var cm = new Ext.grid.ColumnModel([{
    header : '用户ID',
    dataIndex : 'usrId'
}, {
    header : '用户昵称',
    dataIndex : 'usrName'
}, {
    header : '密码',
    dataIndex : 'usrPwd'
}, {
    header : '电子邮件地址',
    dataIndex : 'usrEmail'
}, {
    header : '密码提示问题',
    dataIndex : 'usrPwdQues'
}, {
    header : '密码提示问题答案',
    dataIndex : 'usrPwdAns'
}]);

var reader = new Ext.data.JsonReader({
    root : 'userlist',
    totalProperty : 'totalcount'

}, [{
    name : 'usrId',
    mapping : 'usrId'
}, {
    name : 'usrName',
    mapping : 'usrName'
}, {
    name : 'usrPwd',
    mapping : 'usrPwd'
}, {
    name : 'usrEmail',
    mapping : 'usrEmail'
}, {
    name : 'usrPwdQues',
    mapping : 'usrPwdQues'
}, {
    name : 'usrPwdAns',
    mapping : 'usrPwdAns'
}]);

var ds = new Ext.data.Store({
    proxy : new Ext.data.HttpProxy({
        url : 'searchuser.action'
    // method : 'POST'
    // method : 'GET'
    }),
    reader : reader

});

// var json ={"totalcount" : "1","userlist" : [{"usrId" : "sa","usrName" : "sysadmin"}]}

ds.load();

//ds.loadData(json);
alert(ds.getCount());
// var sm = new Ext.grid.CheckboxSelectionModel();

var grid = new Ext.grid.GridPanel({
    store : ds,
    // sm : sm,
    cm : cm,
    loadMask : {
        msg : ' 数据加载中...'
    },
    autoHeight : true

});

var vp = new Ext.Viewport({
    layout : "border",
    frame : true,
    title : "用户列表",
    autoHeight : true,
    items : [{// Viewport
        region : "north",
        height : 100,
        title : "顶部面板"
    }, {
        region : "south",
        height : 50,
        title : "底部面板"
    }, {
        region : "west",
        width : "20%",
        title : "左边面板"
    }, {
        region : "east",
        width : "20%",
        title : "右边面板"
    }, {
        region : "center",
        title : "中央面板",
        items : [frm = new Ext.FormPanel({
            items : [grid]
        }       /* Eof formpanel */)
        /* Eof border center */]
    }       /* Eof Viewport */]
});
grid.render();

vp.render(document.body);

});
[/code]
直接访问"searchuser.action"
可以得到
{"totalcount" : "1","userlist" : [{"usrId" : "sa","usrName" : "sysadmin"}]}

似乎没有多余数据.

[b]问题补充:[/b]
增加一个页面ul.jsp,里面放[code="xml"] 123/s:a[/code]能获得数据
{"totalcount" : "1","userlist" : [{"usrId" : "sa","usrName" : "sysadmin"}]}

js里面修改url
[code="java"]
var ds = new Ext.data.Store({
proxy : new Ext.data.HttpProxy({
url : 'ul.jsp'
}),
reader : reader
});
[/code]
然后直接点123访问"userlist.jsp" 还是得不到数据.

[b]问题补充:[/b]
得到数据了,增加了一个ul.jsp,然后把数据打印给它,在ul.jsp内用javascript
[code="java"]
ValueStack vs = (ValueStack) request.getAttribute("struts.valueStack");
String userlist = (String) vs.findValue("userlist");

String a = userlist;
response.getWriter().write(a);
[/code]
获取数据并write, 然后把userlist.jsp的url指向ul.jsp,GridPanel就可以得到数据.
似乎问题出在response上,action里面的数据返回时,并没有把数据放到response里面(action里的 response.getWriter().write(userlist);似乎无效).
例如,ul.jsp是一完全空白页面,
[code="java"]
public String searchUser() {
try {
ActionContext ctx = ActionContext.getContext();

HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);

HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);

response.reset();

response.setCharacterEncoding("utf-8");

ComUserInfo comuserinfo = new ComUserInfo();
if (comuserinfo.getUserid() != "") {
List result = uo.searchUser(getUserid());
String totalcount = tc.intToString(result.size());
JsonUtil ju = new JsonUtil();
String jsresult = ju.list2json(result);
this.userlist =
("{\"totalcount\":"+totalcount+",\"userlist\": " + jsresult +" }");
System.out.println(userlist);
response.getWriter().write(userlist);
return SUCCESS;
} else {
ssn.add("errmsg", "User not login yet!");
return ERROR;
}
} catch (Exception e) {
ssn.add("errmsg", e.getMessage());
return ERROR;
}
}
[/code]
打印到ul.jsp的数据是空.

[b]问题补充:[/b]
这个思路
getuser.jsp -> submit -> redirectuserlist.action -> userlist.jsp -> searchuser.action -> userlist.jsp(GridPanel显示数据)
不知道可行否,
"userlist.jsp -> searchuser.action -> userlist.jsp(GridPanel显示数据)" 这里searchuser.action返回的数据userlist.jsp接受不到.

[b]问题补充:[/b]
似乎中间一定要有一个页面用于接收数据.
以下方法可以成功在gridPanel上显示数据
searchuser.action->ul.jsp->userlist.jsp(js中url指向ul.jsp)
不明白.
为何 userlist.jsp->searchuser.action->userlist.jsp不行...

  • 写回答

8条回答 默认 最新

  • iteye_10013 2009-08-18 12:18
    关注

    实在不行的话,你直接在js引用的那个页面把json给print出来.
    然后store.loadData(json)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方