问题描述
前台代码 : store : Ext.create('Ext.data.TreeStore', {root : {id : '00',text : '民星兽药',expanded : true//nodeType : 'async'},proxy : { autoLoad:true,type : 'ajax',url : ctxpath + '/SystemManager/TreeList',reader : {type : 'json',root : 'alltrees'}Action:@Overridepublic String execute() throws Exception {ActionContext actionContext = ActionContext.getContext(); // 取到struts容器Map session = actionContext.getSession(); // 取得sessionUser user = (User) session.get("user");try {alltrees = userServices.listTrees(user.getUsername());for(int i=0;i<alltrees.size();i++){System.out.println(alltrees.get(i).getText());}} catch (Exception e) {e.printStackTrace();}// this.success = true;return SUCCESS;}listTrees对应的业务方法:public List<Menu> listTrees(String username) throws Exception {List<Menu> menus = new ArrayList<Menu>();Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;String sql = "select a.pk_archives,a.archivesname,a.url_name,a.cls,a.leaf,a.pid from drug_user u join drug_role r "+ "on u.role_id = r.limit_id join drug_limit l on r.limit_id = l.role_code "+ "join drug_archives a on l.archives_id = a.limit_id where u.username = ?";try {conn = DBUtil.getConnection();ps = conn.prepareStatement(sql);ps.setString(1, username);rs = ps.executeQuery();while (rs.next()) {Menu menu = new Menu();menu.setId(rs.getString("pk_archives"));menu.setText(rs.getString("archivesname"));menu.setUrl(rs.getString("url_name"));menu.setCls(rs.getString("cls"));menu.setLeaf(Boolean.parseBoolean(rs.getString("leaf")));menu.setParentId(rs.getString("pid"));menus.add(menu);}} catch (Exception e) {e.printStackTrace();} finally {rs.close();ps.close();conn.close();}//System.out.println(menus.size());List<Menu> result = new ArrayList<Menu>();if (menus != null && menus.size() > 0) {for (int i = 0; i < menus.size(); i++) {String id = menus.get(i).getId();for (int j = 0; j < menus.size(); j++) {if (id.equals(menus.get(j).getParentId())) {menus.get(i).getChildren().add(menus.get(j));menus.get(j).setExpanded(true);}}}for (int k = 0; k < menus.size(); k++) {if ("00".equals(menus.get(k).getParentId())) {result.add(menus.get(k));}}}return result;}前台后太都没报错; 谷歌浏览器截取到返回页面的数据:{"alltrees":[{"children":[{"children":[],"cls":"file","expanded":true,"id":"100221","leaf":true,"parentId":"100200","text":"叶子一","url":"/veterinaryDrug/SystemManager/RoleList"}],"cls":"folder","expanded":false,"id":"100200","leaf":false,"parentId":"00","text":"系统管理","url":null},{"children":[],"cls":"folder","expanded":false,"id":"100201","leaf":false,"parentId":"00","text":"销售","url":null}]}现象如图;还有一个问题:为啥登录进来后程序会加载两次treeStore??
解决方案
应该多贴点前端js代码 , 后端java只要返回json字符串即可可以参考下地址 [url]https://gist.github.com/x03570227/2994798[/url]至于请求两次, 可以是你自己autoload一次还有看看其他有没有reload或者tree里面的 expand展开事件处理