初学Extjs,关于gridPanel不显示从Action中接收到的json数据问题
简单的ActionpublicclassjsonAction{privateStringname;privateStringcity;privateDatedate;pu...
简单的Action
public class jsonAction {
private String name;
private String city;
private Date date;
public String execute(){
name="mike";
city="LA";
date=new Date();
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
struts.xml配置文件
<struts>
<package name="default" namespace="/" extends="json-default">
<action name="json" class="action.jsonAction">
<result name="success" type="json"></result>
</action>
</package>
</struts>
Extjs代码:
<div id='d1'></div>
<script type="text/javascript">
Ext.onReady(function(){
var rd=new Ext.data.JsonReader({
fields:[
{name:'city' ,mapping:'city'},
{name:'date',mapping:'date'},
{name:'name',mapping:'name'}
]
});
var store=new Ext.data.JsonStore({
autoLoad:true,
proxy:new Ext.data.HttpProxy({
url:'json.action',
method:'POST'
}),
reader:rd
});
var grid=new Ext.grid.GridPanel({
store:store,
columns:[
{header:'城市',width:175,dataIndex:'city'},
{header:'日期',width:175,dataIndex:'date'},
{header:'姓名',width:175,dataIndex:'name'}
],
height:300,
width:600,
renderTo:'d1'
});
})
</script> 展开
public class jsonAction {
private String name;
private String city;
private Date date;
public String execute(){
name="mike";
city="LA";
date=new Date();
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
struts.xml配置文件
<struts>
<package name="default" namespace="/" extends="json-default">
<action name="json" class="action.jsonAction">
<result name="success" type="json"></result>
</action>
</package>
</struts>
Extjs代码:
<div id='d1'></div>
<script type="text/javascript">
Ext.onReady(function(){
var rd=new Ext.data.JsonReader({
fields:[
{name:'city' ,mapping:'city'},
{name:'date',mapping:'date'},
{name:'name',mapping:'name'}
]
});
var store=new Ext.data.JsonStore({
autoLoad:true,
proxy:new Ext.data.HttpProxy({
url:'json.action',
method:'POST'
}),
reader:rd
});
var grid=new Ext.grid.GridPanel({
store:store,
columns:[
{header:'城市',width:175,dataIndex:'city'},
{header:'日期',width:175,dataIndex:'date'},
{header:'姓名',width:175,dataIndex:'name'}
],
height:300,
width:600,
renderTo:'d1'
});
})
</script> 展开
展开全部
后台json 是 jsonObject 里面有jsonArray 其中一个的名字是你list 的名字 就是前台root的名字 要对应 还有一个就是 totalproterty :是json里面另外一个jsonArray的 名字 是做分页传 记录总数用的
store要根据后台里面存的时候的名字去取 要不取不到
看我的
function NewGridStore(iUrl,iRoot,iTotalProperty,iFields){//获取表store
var store=new Ext.data.JsonStore( {
url:iUrl,
root:iRoot,
method:'post',
totalProperty:iTotalProperty,
fields:iFields
});
return store;
}
我这是封装了的 不过你应该能看懂 返回一个store
后台
List list = testservice.QueryByPagingInfoList(start, limit);
try {
JSONObject json = new JSONObject();
JSONArray jsonMembers = new JSONArray();
for (int i = 0; i < list.size(); i++) {
Publicity publicity = (Publicity) list.get(i);
JSONObject member = new JSONObject();
member.put("id", publicity.getId());
member.put("danwei", publicity.getDanwei());
member.put("jingbanren", publicity.getJingbanren());
member.put("lianxidianhua", publicity.getLianxidianhua());
member.put("liyou", publicity.getLiyou());
member.put("kaishishijian", publicity.getKaishishijian());
member.put("jieshushijian", publicity.getJieshushijian());
member.put("shuliang", publicity.getShuliang());
member.put("guige", publicity.getGuige());
member.put("neirong", publicity.getNeirong());
member.put("quyu", publicity.getQuyu());
jsonMembers.add(member);
}
json.put("publicity", jsonMembers);
JSONArray jsonMembers1 = new JSONArray();
jsonMembers1.add(count);
json.put("totalcount", count);
out.Writer(json);
store要根据后台里面存的时候的名字去取 要不取不到
看我的
function NewGridStore(iUrl,iRoot,iTotalProperty,iFields){//获取表store
var store=new Ext.data.JsonStore( {
url:iUrl,
root:iRoot,
method:'post',
totalProperty:iTotalProperty,
fields:iFields
});
return store;
}
我这是封装了的 不过你应该能看懂 返回一个store
后台
List list = testservice.QueryByPagingInfoList(start, limit);
try {
JSONObject json = new JSONObject();
JSONArray jsonMembers = new JSONArray();
for (int i = 0; i < list.size(); i++) {
Publicity publicity = (Publicity) list.get(i);
JSONObject member = new JSONObject();
member.put("id", publicity.getId());
member.put("danwei", publicity.getDanwei());
member.put("jingbanren", publicity.getJingbanren());
member.put("lianxidianhua", publicity.getLianxidianhua());
member.put("liyou", publicity.getLiyou());
member.put("kaishishijian", publicity.getKaishishijian());
member.put("jieshushijian", publicity.getJieshushijian());
member.put("shuliang", publicity.getShuliang());
member.put("guige", publicity.getGuige());
member.put("neirong", publicity.getNeirong());
member.put("quyu", publicity.getQuyu());
jsonMembers.add(member);
}
json.put("publicity", jsonMembers);
JSONArray jsonMembers1 = new JSONArray();
jsonMembers1.add(count);
json.put("totalcount", count);
out.Writer(json);
展开全部
你是想用ognl的方式来取值吗? 这个好像应该不可以吧, 如果你想在前台渠道action中的值,需要往前台写值;
例如; out.print("{name:'jack',age:20}"); out.flush();
希望可以帮助到您 .
例如; out.print("{name:'jack',age:20}"); out.flush();
希望可以帮助到您 .
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-05-17
展开全部
public class jsonAction extends ActionSupport
先确认确实跳转到Action
先确认确实跳转到Action
追问
确实跳转,并且firebug已经确认接收到了 json字符串,但是就是不再前台显示
追答
new Ext.data.JsonReader( {
root : root,
}, Ext.data.Record.create( [ {
name : 'id'
}, {
name : 'xname'
}])),
是不是少了个root
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询