创建html标签
<div id="tree2"></div>
创建dojo代码
require([ "dojo/store/JsonRest","dijit/Tree","dijit/tree/ObjectStoreModel","dojo/domReady!" ],function(JsonRest,Tree,ObjectStoreModel){ var usGov = new JsonRest({ target: "/dojo/rest/echo/getLazyTree",// dojo为tomcat中配置的Context path,rest为web.xml配置路径 getChildren: function(object){ return this.query({id: object.id}); } }); var model = new ObjectStoreModel({ store: usGov,mayHaveChildren: function(object){ if(object.children == false){// 为叶子节点 return false; }else{// 非叶子节点 return "children" in object; } } }); var tree = new Tree({ model: model,persist: false },"tree2"); tree.startup(); });
rest服务的java代码
bean类
class Org{ String id; String name; boolean children; public String getId() { return id; } public void setId( String id ) { this.id = id; } ...... }
rest类
@Path("/echo") public class Echo { @GET @Path("/getLazyTree") @Produces("application/json") public List< Org > getTreeParent(@Context HttpServletRequest request){ String id = request.getParameter( "id" ); System.out.println("id="+id); if( id == null ){ Org root = new Org(); root.setName( "北京测试" ); root.setId( "root" ); root.setChildren( true ); List< Org > lt = new ArrayList< Org >(); lt.add( root ); return lt; }else{ if( id.equals( "root" ) ){ Org one = new Org(); one.setId( "corp1" ); one.setName( "测试子公司1" ); one.setChildren( true ); Org two = new Org(); two.setId( "corp2" ); two.setName( "测试子公司2" ); two.setChildren( true ); List< Org > subOrgs = new ArrayList< Org >(); subOrgs.add( one ); subOrgs.add( two ); return subOrgs; } if( id.equals( "corp1" ) ){ Org one = new Org(); one.setId( "team1" ); one.setName( "测试车队1" ); one.setChildren( false ); Org two = new Org(); two.setId( "team2" ); two.setName( "测试车队2" ); two.setChildren( false ); List< Org > subOrgs = new ArrayList< Org >(); subOrgs.add( one ); subOrgs.add( two ); return subOrgs; } if( id.equals( "corp2" ) ){ Org one = new Org(); one.setId( "team21" ); one.setName( "测试车队21" ); one.setChildren( false ); Org two = new Org(); two.setId( "team22" ); two.setName( "测试车队22" ); two.setChildren( false ); List< Org > subOrgs = new ArrayList< Org >(); subOrgs.add( one ); subOrgs.add( two ); return subOrgs; } } return null; } }
从头信息看调用过程及返回数据
加载跟节点
加载第一级子节点
调用完成后的页面
点击第一级子节点后的调用过程