Java 使用json数据创建动态树视图-jsp中的dynatree
我在数据库中有这样一个表:Java 使用json数据创建动态树视图-jsp中的dynatree,java,json,jsp,spring-mvc,dynatree,Java,Json,Jsp,Spring Mvc,Dynatree,我在数据库中有这样一个表: id - title - parentID ----------------------------- 1 Root null 2 item 1 1 3 item 2 1 4 item 3 1 5 item 3.1 4 --Root ----item 1 ----item 2 ----item 3 -------i
id - title - parentID
-----------------------------
1 Root null
2 item 1 1
3 item 2 1
4 item 3 1
5 item 3.1 4
--Root
----item 1
----item 2
----item 3
-------item 3.1
这将创建如下内容:
id - title - parentID
-----------------------------
1 Root null
2 item 1 1
3 item 2 1
4 item 3 1
5 item 3.1 4
--Root
----item 1
----item 2
----item 3
-------item 3.1
下面是用于获取根节点及其子节点的控制器:
@RequestMapping(value="/CourtBranch/LoadTreeView", method = RequestMethod.GET)
public void LoadList(@RequestParam("ParentId") String parentID,HttpServletRequest req, HttpServletResponse resp) {
List lst;
if (parentID.equals("Root"))
{
lst = _COURTBRANCH.LoadTreeChildren(null, "Root"); // selects records which have parent=null
}
else
{
lst = _COURTBRANCH.LoadTreeChildren(parentID, "TreeNode"); // selects records which have parent=parentID
}
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
try {
resp.getWriter().print(_gson.toJson(lst));
} catch (IOException e) {
e.printStackTrace();
}
}
这个脚本加载我的根目录:
<script type="text/javascript">
$(function(){
$("#tree").dynatree({
initAjax: {
url: "/CourtBranch/LoadTreeView.json?ParentId=Root",
data: { mode: "funnyMode" }
},
onActivate: function(node) {
$("#echoActive").text(node.data.title);
},
onDeactivate: function(node) {
$("#echoActive").text("-");
}
});
});
</script>
$(函数(){
$(“#树”).动态树({
initAjax:{
url:“/CourtBranch/LoadTreeView.json?ParentId=Root”,
数据:{mode:“funnyMode”}
},
onActivate:函数(节点){
$(“#echoActive”).text(node.data.title);
},
onDeactivate:功能(节点){
$(“#echoActive”).text(“-”);
}
});
});
现在我需要知道如何将root的id发送到我的控制器以获取root的子级
并将它们添加到根节点。
我应该使用appendAjax函数吗?如何操作?基本上,您并不想直接在控制器中执行此操作。(松耦合) 我将创建两个utils类
Tree
和Node
,以管理树和节点:
下面是树类:
public class Tree<T> {
private Node<T> rootElement;
public Tree() {
super();
}
public Node<T> getRootElement() {
return this.rootElement;
}
public void setRootElement(final Node<T> rootElement) {
this.rootElement = rootElement;
}
public List<Node<T>> toList() {
final List<Node<T>> list = new ArrayList<Node<T>>();
walk(this.rootElement, list);
return list;
}
private void walk(final Node<T> element, final List<Node<T>> list) {
list.add(element);
for (final Node<T> data : element.getChildren()) {
walk(data, list);
}
}
}
创建扩展树的类:
public class CourtBranchTree extends Tree<CourtBranch>
public CourtBranchTree{
super();
}
请注意@ResponseBody注释,它将指示Spring将字符串解析为JSON,以便AJAX能够读取它
和您的jsp:
<div id ="cbTree"></div>
<script type ="text/javascript">
$(function(){
$("#cbTree").dynatree({
initAjax:{
url: /CourtBranch/LoadTreeView
},
checkbox: true,
selectMode: 3
});
});
</script>
$(函数(){
$(“#cbTree”).动态树({
initAjax:{
url:/CourtBranch/LoadTreeView
},
复选框:正确,
选择模式:3
});
});
希望这有帮助……我完全按照弗朗索瓦说的做了,而且成功了!谢谢..只是一些小改动,因为出于某种原因,我的JSON没有被解析。我并没有推出MyTree.java和MyNode.java,因为它们是非常基本的简单类
public class MYDocTreeManager {
private MYTree MYTree = new MYTree();
private String sDir = "C:\\Users\\sandeepraj.tandon\\Documents\\DynaTreeFolder";
public void buildMYTree(){
System.out.println("Top Level Starts");
File[] faFiles = new File(sDir).listFiles();
int key = 1;
for(File file: faFiles){
MYNode MYNode = new MYNode();
MYNode.setTitle(file.getName());
MYNode.setTooltip(file.getName());
MYNode.setKey(String.valueOf(key));
MYTree.addNode(MYNode);
if(file.getName().matches("^(.*?)")){
System.out.println(file.getName()+" is a file at top level");
}
if(file.isDirectory()){
MYNode.isFolder(true);
//System.out.println(file.getAbsolutePath()+"is a directory at top level");
//String treeJson = file.getAbsolutePath()+"is a directory at top level";
addChildToNodeRec(MYNode,file.getAbsolutePath(), key);
}
key++;
}
}
private void addChildToNodeRec(MYNode MYNode,String currDirPath, int key){
System.out.println("Scanning under "+MYNode.getTitle());
int childKey = 1;
File[] faFiles = new File(currDirPath).listFiles();
for(File file: faFiles){
MYNode childNode = new MYNode();
childNode.setTitle(file.getName());
MYNode.setKey(String.valueOf(key)+"."+childKey);
MYNode.addChild(childNode);
if(file.getName().matches("^(.*?)")){
System.out.println("Found a document "+file.getName());
}
if(file.isDirectory()){
System.out.println("Found a directory "+file.getName());
MYNode.isFolder(true);
addChildToNodeRec(childNode,file.getAbsolutePath(), childKey);
}
childKey++;
}
}
/**
* Get the JSON object for the tree built
* @return
*/
public String treeToJason() {
StringBuffer json =new StringBuffer();
json.append("[\r");
int topNodeCount = 0;
for (MYNode topNode:MYTree.getTopLevelNodes()){
//System.out.println("topNode title :"+topNode.getTitle());
//json = json.append("topNode title :"+topNode.getTitle()+"\n");
if(topNodeCount==0){
json.append("{\"title\": \"" + topNode.getTitle() + "\", \"key\": \"" + topNode.getKey() + "\", \"children\": [\r");
} else {
json.append(", \n {\"title\": \"" + topNode.getTitle() + "\", \"key\": \"" + topNode.getKey() + "\", \"children\": [\r");
}
//json = json.append("topNode title :"+topNode.getTitle()+"\n");
childNodeJson(json, topNode);
topNodeCount++;
}
json.append("]");
return json.toString();
// return MYTree.toJson(MYTree);
}
private void childNodeJson(StringBuffer json, MYNode node) {
int childCount = 0;
for (MYNode child : node.getChildren()){
//System.out.println("Child No."+childCount+" of "+topNode.getTitle()+" is :"+child.getTitle()+"\n");
//json.append("Child No."+childCount+" of "+topNode.getTitle()+" is :"+child.getTitle()+"\n");
if (childCount == 0) {
json.append("\t\t");
json.append("{\"title\": \"" + child.getTitle() + "\", \"key\": \"" + child.getKey() + "\"");
} else {
json.append(",");
json.append("\n\t\t");
json.append("{\"title\": \"" + child.getTitle() + "\", \"key\": \"" + child.getKey() + "\"");
}
if(child.getChildren().size()>0){
json.append(",\"children\": [\r");
childNodeJson(json, child);
} else {
json.append("}");
}
childCount++;
}
json.append("\n\t\t]}");
}
public static void main(String args[]){
MYDocTreeManager MYDocTreeManager = new MYDocTreeManager();
MYDocTreeManager.buildMYTree();
System.out.println(MYDocTreeManager.treeToJason());
}
}
如这里所讨论的,更改了我的实现
<div id ="cbTree"></div>
<script type ="text/javascript">
$(function(){
$("#cbTree").dynatree({
initAjax:{
url: /CourtBranch/LoadTreeView
},
checkbox: true,
selectMode: 3
});
});
</script>
public class MYDocTreeManager {
private MYTree MYTree = new MYTree();
private String sDir = "C:\\Users\\sandeepraj.tandon\\Documents\\DynaTreeFolder";
public void buildMYTree(){
System.out.println("Top Level Starts");
File[] faFiles = new File(sDir).listFiles();
int key = 1;
for(File file: faFiles){
MYNode MYNode = new MYNode();
MYNode.setTitle(file.getName());
MYNode.setTooltip(file.getName());
MYNode.setKey(String.valueOf(key));
MYTree.addNode(MYNode);
if(file.getName().matches("^(.*?)")){
System.out.println(file.getName()+" is a file at top level");
}
if(file.isDirectory()){
MYNode.isFolder(true);
//System.out.println(file.getAbsolutePath()+"is a directory at top level");
//String treeJson = file.getAbsolutePath()+"is a directory at top level";
addChildToNodeRec(MYNode,file.getAbsolutePath(), key);
}
key++;
}
}
private void addChildToNodeRec(MYNode MYNode,String currDirPath, int key){
System.out.println("Scanning under "+MYNode.getTitle());
int childKey = 1;
File[] faFiles = new File(currDirPath).listFiles();
for(File file: faFiles){
MYNode childNode = new MYNode();
childNode.setTitle(file.getName());
MYNode.setKey(String.valueOf(key)+"."+childKey);
MYNode.addChild(childNode);
if(file.getName().matches("^(.*?)")){
System.out.println("Found a document "+file.getName());
}
if(file.isDirectory()){
System.out.println("Found a directory "+file.getName());
MYNode.isFolder(true);
addChildToNodeRec(childNode,file.getAbsolutePath(), childKey);
}
childKey++;
}
}
/**
* Get the JSON object for the tree built
* @return
*/
public String treeToJason() {
StringBuffer json =new StringBuffer();
json.append("[\r");
int topNodeCount = 0;
for (MYNode topNode:MYTree.getTopLevelNodes()){
//System.out.println("topNode title :"+topNode.getTitle());
//json = json.append("topNode title :"+topNode.getTitle()+"\n");
if(topNodeCount==0){
json.append("{\"title\": \"" + topNode.getTitle() + "\", \"key\": \"" + topNode.getKey() + "\", \"children\": [\r");
} else {
json.append(", \n {\"title\": \"" + topNode.getTitle() + "\", \"key\": \"" + topNode.getKey() + "\", \"children\": [\r");
}
//json = json.append("topNode title :"+topNode.getTitle()+"\n");
childNodeJson(json, topNode);
topNodeCount++;
}
json.append("]");
return json.toString();
// return MYTree.toJson(MYTree);
}
private void childNodeJson(StringBuffer json, MYNode node) {
int childCount = 0;
for (MYNode child : node.getChildren()){
//System.out.println("Child No."+childCount+" of "+topNode.getTitle()+" is :"+child.getTitle()+"\n");
//json.append("Child No."+childCount+" of "+topNode.getTitle()+" is :"+child.getTitle()+"\n");
if (childCount == 0) {
json.append("\t\t");
json.append("{\"title\": \"" + child.getTitle() + "\", \"key\": \"" + child.getKey() + "\"");
} else {
json.append(",");
json.append("\n\t\t");
json.append("{\"title\": \"" + child.getTitle() + "\", \"key\": \"" + child.getKey() + "\"");
}
if(child.getChildren().size()>0){
json.append(",\"children\": [\r");
childNodeJson(json, child);
} else {
json.append("}");
}
childCount++;
}
json.append("\n\t\t]}");
}
public static void main(String args[]){
MYDocTreeManager MYDocTreeManager = new MYDocTreeManager();
MYDocTreeManager.buildMYTree();
System.out.println(MYDocTreeManager.treeToJason());
}