JAVA后台输出JSON如何跳转路径??

lylshr 2011-01-22 09:35:50
我现在前台用的是EXT JS接收后台ACTION传过来的数据,我后台必须输出JSON的字符串到前台去获取。
我的ACTION这样跳转路径好像就有一些问题了。
List<JSONObject> jsonlist =new ArrayList<JSONObject>();
for(Object object: userList){
JSONObject json= JSONObject.fromObject(object);
jsonlist.add(json);
}
String json=jsonlist.toString();
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
// StringBuffer sb = new StringBuffer();
// sb.append("<script language=\"javaScript\">\r\n");
// sb.append("location.href=\"/Shilikaifa2/userd.do?action=queryUser\";");
// sb.append("</script>\r\n");
out.print(json);
// out.println(sb.toString());
out.flush();
out.close();

我这样也不可以用return.mapping.forward去重定向一个路径,这样传值好像也存在问题。
我的前台用EXT JS接收是这样的,不知道有没有错

    var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'queryuser.action'}),
id:'userList',
reader: new Ext.data.JsonReader({
userCode: 'userCode',
userName: 'userName'
},
});

麻烦会的高手帮忙看一下。、
...全文
863 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lylshr 2011-01-24
  • 打赏
  • 举报
回复
12楼我如果用STRUTS1该怎么写呢。
Inhibitory 2011-01-23
  • 打赏
  • 举报
回复
Struts的AJAX,要么返回null,要么写个void的方法,不能使用Struts的跳转。
fq1798 2011-01-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ku_wan 的回复:]
Action的代码:

Java code
//显示
public void showManagers(){
if(operation==null||operation.equals("show")||operation.equals("")){
this.pageCount = ims.getPageCount(); ……
[/Quote]

感觉和 ajax 差不多啊,,这个应该可以啊
qinhualee 2011-01-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lylshr 的回复:]
问题是我现在这个ACTION后台到底写了有错啊,好像不能传JSON到前台去。
[/Quote]

把你的 json 输出来看看有没有错
Ku_wan 2011-01-22
  • 打赏
  • 举报
回复
Action的代码:
//显示
public void showManagers(){
if(operation==null||operation.equals("show")||operation.equals("")){
this.pageCount = ims.getPageCount();
this.changPage();
this.managers = ims.findManagers(this.pageNow);

//构造Json数据
JSONArray array = JSONArray.fromObject(this.managers);
System.out.println(array.toString());
this.jsonString = "{\"totalCount\":"+this.managers.size()+",\"results\":"+array.toString()+"}";

try {
ServletActionContext.getResponse().getWriter().print(this.jsonString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


前台ExtJS调用代码:
	var store = new Ext.data.JsonStore({
proxy:new Ext.data.HttpProxy({
url:'ManagerAction!showManagers.action',
method:'POST'
}),
totalProperty:'totalCount',
root:'results',
fields:["id","account","password","managerLevel"]
});
Ku_wan 2011-01-22
  • 打赏
  • 举报
回复
其实你们都错了,直接不用返回任何东西,写一个void方法给其调用就行了,前两天刚刚研究出来的。
微甜灬呼吸 2011-01-22
  • 打赏
  • 举报
回复
一直觉得json没啥用
满意 2011-01-22
  • 打赏
  • 举报
回复
return null; 就Ok啊
lylshr 2011-01-22
  • 打赏
  • 举报
回复
问题是我现在这个ACTION后台到底写了有错啊,好像不能传JSON到前台去。
Neetgo 2011-01-22
  • 打赏
  • 举报
回复
  
var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'queryuser.action'}),
id:'userList',
reader: new Ext.data.JsonReader({
userCode: 'userCode',
userName: 'userName'
},
});


这样取数据,是保存在store里面的,可以查查EXT的Store怎样使用。
Neetgo 2011-01-22
  • 打赏
  • 举报
回复
从页面到后台取JSON数据,可以通过两次调用。
第一个方法负责页面跳转,跳转到指定页面A.jsp,
A.jsp里面用脚本通过Ajax请求,执行后台的另一个方法,该方法返回JSON字符串到前台的脚本方法里面就可拿到值了。
Ku_wan 2011-01-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lylshr 的回复:]
7楼你的前台代码没写错吧

Java code
var store = new Ext.data.JsonStore({
proxy:new Ext.data.HttpProxy({
url:'ManagerAction!showManagers.action',
method:'POST'
}),
……
[/Quote]

晕死,struts2 的动态调用(DMI) 。
lylshr 2011-01-22
  • 打赏
  • 举报
回复
7楼你的前台代码没写错吧
 var store = new Ext.data.JsonStore({
proxy:new Ext.data.HttpProxy({
url:'ManagerAction!showManagers.action',
method:'POST'
}),
totalProperty:'totalCount',
root:'results',
fields:["id","account","password","managerLevel"]
});


url:'ManagerAction!showManagers.action'这里怎么还可以写感叹号呢??
readme1988 2011-01-22
  • 打赏
  • 举报
回复
首先你的思路是错的,用ext做页面显示用的ajax异步调用可以直接在页面显示出来,所以通过一个action完成跳转和显示是不对的,应该先用一个action跳转到指定页面,然后页面就可以通过异步处理直接调用url指定的action。所以要增加action修改struct的xml。
其次,datastore要有一个store.load(),这样在panel引用的时候才能显示数据。
好久没用struts1了,有点不习惯了。。。。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧