如何配置,需要代码,注释。高手帮忙~~~~初学~

解决方案 »

  1.   

    把树结构作成json格式发给页面,然后在页面上用js解析。
    用jQuery和相应的插件,这个不太难搞的。
      

  2.   

    菜单可以用struts2+extjs+json来实现,下面是个简单的例子
    1.定义菜单类package com.lab;import java.util.List;public class Menu {
    private int id;
    private String text;
    private boolean leaf;
    private String cls;
    private List<Menu> children; public int getId() {
    return id;
    } public void setId(int id) {
    this.id = id;
    } public String getText() {
    return text;
    } public void setText(String text) {
    this.text = text;
    } public boolean isLeaf() {
    return leaf;
    } public void setLeaf(boolean leaf) {
    this.leaf = leaf;
    } public String getCls() {
    return cls;
    } public void setCls(String cls) {
    this.cls = cls;
    } public List<Menu> getChildren() {
    return children;
    } public void setChildren(List<Menu> children) {
    this.children = children;
    }
    }2.定义生成菜单的action,向客户端返回json格式的数据package com.lab;import java.util.ArrayList;
    import java.util.List;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;import net.sf.json.JSONArray;public class MenuAction {
    private static final Log LOG=LogFactory.getLog(MenuAction.class); 
    private String menuString; private List<Menu> menus; public String execute() { menus = new ArrayList<Menu>(); Menu benz = new Menu();
    benz.setText("Benz");
    benz.setCls("folder");
    benz.setLeaf(false);
    benz.setId(10);
    menus.add(benz); List<Menu> benzList = new ArrayList<Menu>();
    benz.setChildren(benzList); Menu menu;
    menu = new Menu();
    menu.setText("S600");
    menu.setCls("file");
    menu.setLeaf(true);
    menu.setId(11);
    benzList.add(menu);
    menu = new Menu();
    menu.setText("SLK200");
    menu.setCls("file");
    menu.setLeaf(true);
    menu.setId(12);
    benzList.add(menu); Menu bmw = new Menu();
    bmw.setText("BMW");
    bmw.setCls("folder");
    bmw.setLeaf(false);
    bmw.setId(20);
    menus.add(bmw); List<Menu> bmwList = new ArrayList<Menu>();
    bmw.setChildren(bmwList); menu = new Menu();
    menu.setText("325i");
    menu.setCls("file");
    menu.setLeaf(true);
    menu.setId(21);
    bmwList.add(menu); menu = new Menu();
    menu.setText("X5");
    menu.setCls("file");
    menu.setLeaf(true);
    menu.setId(22);
    bmwList.add(menu); JSONArray jsonObject = JSONArray.fromObject(menus);
    try {
    menuString = jsonObject.toString();
    } catch (Exception e) {
    menuString = "ss";
    }
            LOG.info(menuString);
    return "success";
    } public String getMenuString() {
    return menuString;
    } public void setMenuString(String menuString) {
    this.menuString = menuString;
    }
    }
    3.修改struts.xml文件<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <package name="person" extends="struts-default">
    <action name="menus" method="execute" class="com.lab.MenuAction">
    <result>/menu.jsp</result></action>
    <action name="fileUpload" class="com.lab.UploadAction"></action></package>
    <constant name="struts.devMode" value="true"></constant></struts>    
    4.编写menu.jsp,这个页面主要是用来把action的输出传递给请求页面。如果用struts2的json插件的话就不需要这个页面。<%@ taglib prefix="s" uri="/struts-tags" %>   
    <s:property value="menuString" escape="false"/> 
    5.请求页面<body>      
    <script type="text/javascript">
    /*
     * Ext JS Library 2.3.0
     * Copyright(c) 2006-2009, Ext JS, LLC.
     * [email protected]
     * 
     * http://extjs.com/license
     */Ext.onReady(function(){
        // shorthand
        var Tree = Ext.tree;
        
        var tree = new Tree.TreePanel({
            el:'tree-div',
            useArrows:true,
            autoScroll:true,
            animate:true,
            enableDD:true,
            containerScroll: true,        // auto create TreeLoader
            dataUrl: 'http://localhost:10000/extjs_tree/menus.action',        root: {
                nodeType: 'async',
                text: 'Ext JS',
                draggable:false,
                id:'source'
            }
        });    // render the tree
        tree.render();
        tree.getRootNode().expand();
    });
    </script>  
    <div id="tree-div" style="overflow:auto; height:300px;width:250px;border:1px solid #c3daf9;"></div>   
       
    </body>