==================
tree.html
==================
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>test tree21</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
    <!-- GC -->
  <!-- LIBS -->
  <script type="text/javascript" src="../../adapter/ext/ext-base.js"></script>
  <!-- ENDLIBS -->
    <script type="text/javascript" src="../../ext-all.js"></script>
<script type="text/javascript" src="tree.js"></script>
<!-- Common Styles for the examples -->
<link rel="stylesheet" type="text/css" href="../examples.css" />
</head>
<body>
<script type="text/javascript" src="../examples.js"></script><!-- EXAMPLES -->
树型
<div id="tree" style="overflow:auto; height:300px;width:250px;border:1px solid #c3daf9;"></div>
</body>
</html>
========================
tree.js
========================
Ext.onReady(function() {      var Tree = Ext.tree;
      var tree = new Tree.TreePanel( {            el : 'tree',//目标div容器
            autoScroll : true,
            animate : true,
            enableDD : true,
            containerScroll : true,
            loader : new Tree.TreeLoader( {
                  dataUrl : 'tree.jsp'
            })
      });
      
      var root = new Tree.AsyncTreeNode( {
            id:'1',
            text : '中国'
      });      tree.setRootNode(root);
      tree.render();
      root.expand();
});
=================================
tree.jsp
=================================
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'tree21.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body>
    <% 
     StringBuffer ss = new StringBuffer();
     ss.append("[{id:'10',text:'beijing',children:[");
     ss.append("{id:'1010',text:'dongcheng',leaf:true},");
     ss.append(" {id:'1011',text:'01-02',children:[");
     ss.append(" {id:'101110',text:'01-02-01',leaf:true},");
     ss.append(" {id:'101111',text:'01-02-02',leaf:true}]},");
     ss.append(" {id:'1012',text:'01-03',leaf:true} ]},");
     ss.append(" {id:'11',text:'02',leaf:true}]");
     //out.print(ss.toString());
     out.write(ss.toString());
    %>
  </body>
</html>
==========================
tree.txt
==========================
[
    {id:'10',text:'beijing',children:[
        {id:'1010',text:'dongcheng',leaf:true},
        {id:'1011',text:'01-02',children:[
            {id:'101110',text:'01-02-01',leaf:true},
            {id:'101111',text:'01-02-02',leaf:true}
        ]},
        {id:'1012',text:'01-03',leaf:true}
    ]},
    {id:'11',text:'02',leaf:true}
]
==========================
三个文件在同一目录下,程序放到tomact下能跑起来
但是只显示根节点,当我把tree.js 下的dataUrl : 'tree.jsp'改成静态的dataUrl : 'tree.txt'时,树就能完整展现
还有个问题,从数据库中取得数据怎么编程tree.txt那样的数组

解决方案 »

  1.   

    有工具可以吧类直接转成JSON格式的
    不过我没用过 呵呵
      

  2.   

    json 有规定的,它只能有哪个格式,所以你得把前面无用的输出和后面的无用输出都去掉。
    请参考
        <% 
       out.clearBuffer(); // 这里清除掉前面的任何输出 
         StringBuffer ss = new StringBuffer(); 
         ss.append("[{id:'10',text:'beijing',children:["); 
         ss.append("{id:'1010',text:'dongcheng',leaf:true},"); 
         ss.append(" {id:'1011',text:'01-02',children:["); 
         ss.append(" {id:'101110',text:'01-02-01',leaf:true},"); 
         ss.append(" {id:'101111',text:'01-02-02',leaf:true}]},"); 
         ss.append(" {id:'1012',text:'01-03',leaf:true} ]},"); 
         ss.append(" {id:'11',text:'02',leaf:true}]"); 
         //out.print(ss.toString()); 
         out.write(ss.toString()); 
    // 后面的代码全部删除,不要了
        %> 检验方式,单独运行这个页面,在IE里鼠标右键,查看页面源代码,应该只有.txt里面的内容和格式,不应该有<html>的任何内容才对!!
      

  3.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
       <head> 
         <base href=" <%=basePath%>"> 
         
         <title>My JSP 'tree21.jsp' starting page </title> 
         
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">     
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
    <meta http-equiv="description" content="This is my page"> 
    <!-- 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    -->    </head> 
       
       <body> 
    还有   </body> 
    </html> 
    这些都是无用的代码,都是JSON所不允许的格式,当然你的tree也就不能使用了!!
      

  4.   

    问题,解决了,是json格式有问题,有时间把代码帖上来,树都完成了,