问题描述
新手刚接触flexigrid,数据显示不出来,求各位解答!谢谢。页面js代码:JScript code $("#showData").flexigrid({ url: 'flexi/flexi!getData.action', //链接 dataType:'json', colModel : [ {display: '编号', name : 'id', width : 50, sortable : true, align: 'center'}, {display: '姓名', name : 'username', width : 100, sortable : true, align: 'center'}, {display: '年龄', name : 'age', width : 50, sortable : true, align: 'center', hide: false}, {display: '性别', name : 'sex', width : 50, sortable : true, align: 'center', hide: false}, {display: '邮编', name : 'email', width : 150, sortable : true, align: 'center', hide: false}, ], buttons : [ {name: '增加', bclass: 'add'}, {name: '删除', bclass: 'delete'}, {name: '修改', bclass: 'edit'}, ], searchitems : [ {display: '姓名', name : 'username', isdefault: true}, {display: '年龄', name : 'age'} ], sortname: "id", sortorder: "asc", title: '用户管理', height: 200 , width:700, showTableToggleBtn : true, usepager:true,});struts.xml代码:XML code <package name="json" namespace="/flexi" extends="json-default"> <action name="flexi" class="flexi.action.MyFlexiGridAction"> <result type="json"/> </action> </package>action代码:Java code public class MyFlexiGridAction extends ActionSupport { private Integer page;//当前页 private Integer totle;//共几页 private Integer rp;//每页显示条目 private List<People> rows; public String getData() { System.out.print("获取数据..."); rows=new ArrayList<People>(); for(int i=0;i<27;i++){ People p=new People(); p.setId("No."+i); p.setUsername("黄"+i); p.setAge("i"); p.setSex("男"); p.setEmail("hcw"+i+"@126.com"); rows.add(p); } int totlePage=rows.size()/this.getRp(); if(rows.size()%this.getRp()>0) totlePage +=1; this.setTotle(totlePage); System.out.print("共"+rows.size()+"条n"); return null; } @JSON(name="page") public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } @JSON(name="totle") public Integer getTotle() { return totle; } public void setTotle(Integer totle) { this.totle = totle; } @JSON(name="rp") public Integer getRp() { return rp; } public void setRp(Integer rp) { this.rp = rp; } @JSON(name="rows") public List<People> getRows() { return rows; } public void setRows(List<People> rows) { this.rows = rows; }}People.java里面的字段是: private String id;private String username;private String age;private String sex;private String email;set和get省略。 问题补充:hyj1254 写道
解决方案
public class MyFlexiGridAction implements ServletResponseAware{ public void getData() { //拼好前台需要的数据格式 ....... //假设最终数据是Map map = ..... //然后 PrintWriter out = getWriter(); out.write(JSONSerializer.toJSON(map).toString()); }private PrintWriter getWriter() {response.setHeader("charset", "UTF-8");response.setCharacterEncoding("UTF-8");response.setContentType("text/xml;charset=UTF-8");try {return response.getWriter();} catch (IOException e) {return null;}}//}
解决方案二:
用response.getPrintWriter().write();吧,把结果输出到客户端,这样行。你那种配置方式没有用过,不知道有没有这种效果。