求一个例子:extjs树形菜单动态从后台加载节点的实例

本人目前在自学extjs,读的相关书籍是《深入浅出extjs》但是对于本书中的树形菜单动态加载节点以及相关节点的加载和点击事件,总是不能写出满意的例子来,希望各位中能抽出时间的帮忙写一个完整的实例,(可以写成jsp+servlet的,容易一些)要求:
首先,加载所有节点,然后再点击节点的时候能够进行请求提交。当然了,所有的节点都是动态从servlet中取到的。谢谢了。。。

3个回答

TreeSevice
[code="java"]
package name.sencha;

public class TreeService {
public String getLevel1Nodes(){
return "[{text:\"北京\", id:\"1\"},{text:\"吉林\", id:\"2\"}]";
}

public String getLevel2Nodes(){
    return "[{text:\"丰台\", id:\"1-1\"},{text:\"海淀\", id:\"1-2\", leaf:true},{text:\"东城\", id:\"1-3\", leaf:true},{text:\"西城\", id:\"1-4\", leaf:true}]";
}

public String getLevel3Nodes(){
    return "[{text:\"长春\", id:\"2-1\", leaf:true},{text:\"吉林\", id:\"2-2\", leaf:true},{text:\"白山\", id:\"2-3\", leaf:true},{text:\"白城\", id:\"2-4\", leaf:true}]";
}

public String getLevel4Nodes(){
    return "[{text:\"丰台1区\", id:\"1-1-1\", leaf:true},{text:\"丰台2区\", id:\"1-1-2\", leaf:true},{text:\"丰台3区\", id:\"1-1-3\", leaf:true},{text:\"丰台小学\", id:\"1-1-4\", leaf:true}]";
}

public String getTreeNodeS(String id){
    if(id==null||id.length()==0){
        return "[]";
    }else if(id.equals("0")){
        return getLevel1Nodes();
    }else if(id.equals("1")){
        return getLevel2Nodes();
    }else if(id.equals("2")){
        return getLevel3Nodes();
    }else{
        return getLevel4Nodes();
    }
}

}

[/code]

TreeServlet :
[code="java"]
package name.sencha;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TreeServlet extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response){
String id = request.getParameter("id");
if(id!=null&&id.length()>0){
TreeService service = new TreeService();
try {
response.setCharacterEncoding("UTF-8");
response.getWriter().write(service.getTreeNodeS(id));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response){
    doPost(request, response);
}

}

[/code]

web.xml:
[code="java"]

treeservlet
name.sencha.TreeServlet


treeservlet
/tree

[/code]

html :
[code="html"]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



Ext.onReady(function(){ Ext.BLANK_IMAGE_URL = "../../ext-3.0.0/resources/images/default/s.gif"; var rootNode = new Ext.tree.AsyncTreeNode({ text : "根", id : "0", expanded : true }); var tree = new Ext.tree.TreePanel({ root : rootNode, loader : new Ext.tree.TreeLoader({ dataUrl : "../../tree" }), width : 200, height : 400 }); tree.on("beforeload" , function(node){ tree.loader.baseParams.id = node.id; }); tree.on("click", function(node){ Ext.Ajax.request({ url : "../../tree", params : {id:1}, success : function(res){ var txt = res.responseText; var obj = Ext.decode(txt); alert(obj.length); } }); }); tree.render(Ext.get("tree_div")); });

Insert title here



[/code]

那我只能说是你处理的有问题了,在我这儿是跑的好好的,我才把代码帖上来了。

这几个码分别写成什么文件,放在什么目录下

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问