Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically you don't really want to do that straight into your controller. (loose coupling)</p> <p>I'd create 2 utils classes <code>Tree&lt;T&gt;</code> and <code>Node&lt;T&gt;</code> to manage your tree and nodes:</p> <p>Here is the tree class:</p> <pre><code>public class Tree&lt;T&gt; { private Node&lt;T&gt; rootElement; public Tree() { super(); } public Node&lt;T&gt; getRootElement() { return this.rootElement; } public void setRootElement(final Node&lt;T&gt; rootElement) { this.rootElement = rootElement; } public List&lt;Node&lt;T&gt;&gt; toList() { final List&lt;Node&lt;T&gt;&gt; list = new ArrayList&lt;Node&lt;T&gt;&gt;(); walk(this.rootElement, list); return list; } private void walk(final Node&lt;T&gt; element, final List&lt;Node&lt;T&gt;&gt; list) { list.add(element); for (final Node&lt;T&gt; data : element.getChildren()) { walk(data, list); } } } </code></pre> <p>And the node class with some helper methods</p> <pre><code>public class Node&lt;T&gt; { private T data; private List&lt;Node&lt;T&gt;&gt; children; public Node() { super(); } public Node(final T data) { this(); setData(data); } public Boolean hasChildren() { return this.children.size() != 0; } public List&lt;Node&lt;T&gt;&gt; getChildren() { if (this.children == null) { return new ArrayList&lt;Node&lt;T&gt;&gt;(); } return this.children; } public void setChildren(final List&lt;Node&lt;T&gt;&gt; children) { this.children = children; } public int getNumberOfChildren() { return this.children.size(); } public void addChild(final Node&lt;T&gt; child) { if (this.children == null) { this.children = new ArrayList&lt;Node&lt;T&gt;&gt;(); } this.children.add(child); } public void insertChildAt(final int index, final Node&lt;T&gt; child) throws IndexOutOfBoundsException { if (index == getNumberOfChildren()) { addChild(child); return; } else { this.children.get(index); this.children.add(index, child); } } public void removeChildAt(final int index) throws IndexOutOfBoundsException { this.children.remove(index); } public T getData() { return this.data; } public void setData(final T data) { this.data = data; } } </code></pre> <p>Then I'd have your type of tree extending the tree. based on your controller CourtBranch</p> <p>Suppose your CourtBranch model has the following structure (I'm using hibernate + jpa):</p> <pre><code>@Entity @Table public class CourtBranch private String id; private String name; private Long partentId; //getters setters etc... </code></pre> <p>Create a class that extends the Tree:</p> <pre><code>public class CourtBranchTree extends Tree&lt;CourtBranch&gt; public CourtBranchTree{ super(); } </code></pre> <p>Now create your TreeGridResponse class:</p> <pre><code>public class TreeGridResponse { //inject the service to get the id of your model @Resource CourtBranchService cbService; //if you are using a repository for the db queries: @Resource CourtBranchRepository cbRepo; public TreeGridResponse(){ } //returning the tree as a JSON to use AJAX public String cbBTreeAsJson(final CourtBranchTree tree){ final StringBuffer sb = new StringBuffer(); final CourtBranch root = tree.getRootElement().getData(); sb.append("[\r {\"title\": \"" + root.getName() + "\", \"key\": \"" + root.getId() + "\", \"children\": [\r"); final List&lt;Node&lt;CourtBranch&gt;&gt; children = tree.getRootElement().getChildren(); loopforChildre(sb, children); sb.append("]"); return sb.toString(); } private StringBuffer loopForChildren(final StringBuffer sb, final List&lt;Node&lt;UserRight&gt;&gt; children) { for (int i = 0; i &lt; children.size(); i++) { final Node&lt;courtBranch&gt; childElement = children.get(i); if (i == 0) { sb.append("{\"title\": \"" + childElement.getData().getName() + "\", \"key\": \"" + childElement.getData().getId() + "\""); } else { sb.append(", {\"title\": \"" + childElement.getData().getName() + "\", \"key\": \"" + childElement.getData().getId() + "\""); } if (childElement.hasChildren()) { sb.append(", \"children\": [\r"); loopForChildren(sb, childElement.getChildren()); } else { sb.append("}"); } } sb.append("]}"); return sb; } public CourtBranchTree get() { final CourtBranchTreetree tree = new CourtBranchTree(); final Node&lt;CourtBranch&gt; root = new Node&lt;CourtBranch&gt; (this.cbRepo.findOne(Long.valueOf(1)));//gets your root getRecursive(root, tree); tree.setRootElement(root); return tree; } private void getRecursive(final Node&lt;CourtBranch&gt; courtBranch, final CourtBranchTree tree) { final List&lt;CourtBranch&gt; children = this.cbService.findCourtBranchByParentId(courtBranch.getData().getId()); final List&lt;Node&lt;CourtBranch&gt;&gt; childElements = new ArrayList&lt;Node&lt;CourtBranch&gt;&gt;(); for (final CourtBranch childCourtBranch : children) { final Node&lt;CourtBranch&gt; childElement = new Node&lt;CourtBranch&gt;(childUserRight); childElements.add(childElement); getRecursive(childElement, tree); } courtBranch.setChildren(childElements); } } </code></pre> <p>Register the TreeGridResponse in your config to get the bean and inject it into your controller.</p> <pre><code>@Controller public class CBController @Resource private TreeGridResponse gridResponse; @RequestMapping(value="/CourtBranch/LoadTreeView", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public String cbTree() { return this.gridResponse.cbBTreeAsJson(this.gridResponse.get()); } </code></pre> <p>Note the @ResponseBody annotation that will indicate Spring to parse your string into JSON so that your AJAX can read it.</p> <p>And your jsp:</p> <pre><code>&lt;div id ="cbTree"&gt;&lt;/div&gt; &lt;script type ="text/javascript"&gt; $(function(){ $("#cbTree").dynatree({ initAjax:{ url: /CourtBranch/LoadTreeView }, checkbox: true, selectMode: 3 }); }); &lt;/script&gt; </code></pre> <p>Hope this helped...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload