需要生成:
http://www.softcomplex.com/products/tigra_menu_tree/ 

最终生成的菜单:

 

首先按需求定义了一个model.

package com.jxlt.db.parse; 
import java.io.Serializable; 

public class TreeModel implements Serializable { 
        private static final long serialVersionUID = 7896562831509010976L; 

        private String code; 
        private String title; 
        private String url; 
        private boolean leaf; 
        private int level; 

        /** 
        * @return Returns the leaf. 
        */ 
        public boolean isLeaf() { 
                return leaf; 
        } 

        /** 
        * @param leaf 
        *            The leaf to set. 
        */ 
        public void setLeaf(boolean leaf) { 
                this.leaf = leaf; 
        } 

        /** 
        * @return Returns the title. 
        */ 
        public String getTitle() { 
                return title; 
        } 

        /** 
        * @param title 
        *            The title to set. 
        */ 
        public void setTitle(String title) { 
                this.title = title; 
        } 

        /** 
        * @return Returns the url. 
        */ 
        public String getUrl() { 
                return url; 
        } 

        /** 
        * @param url 
        *            The url to set. 
        */ 
        public void setUrl(String url) { 
                this.url = url; 
        } 

        /** 
        * @return Returns the code. 
        */ 
        public String getCode() { 
                return code; 
        } 

        /** 
        * @param code 
        *            The code to set. 
        */ 
        public void setCode(String code) { 
                this.code = code; 
        } 

        /** 
        * @return Returns the level. 
        */ 
        public int getLevel() { 
                return level; 
        } 

        /** 
        * @param level 
        *            The level to set. 
        */ 
        public void setLevel(int level) { 
                this.level = level; 
        } 

}

获取内容:

package com.jxlt.db.parse; 

import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.List; 

import com.jxlt.db.util.DataSourceUtils; 

public class ParseTree { 
        private static final String sql = "select t.bz_code code, 
                                          t.bz_title itle, t.bz_content content "
                                 + "  from bz_czzs t " 
                                 + "  order by t.bz_code"; 

        private static final String sql2 = "select t.bz_code code, 
                                           t.bz_title title, 
                                           t.bz_content content " 
                                           + "  from bz_czzs t " 
                                 + " where substr(t.bz_code, 0, 3) = '002'" 
                                 + " order by t.bz_code"; 

        public static List parse() { 
                Connection conn = null; 
                Statement st = null; 
                ResultSet rs = null; 
                List list = new ArrayList(); 
                try { 
                        conn = DataSourceUtils.getConnection(); 
                        st = conn.createStatement(); 

                        rs = st.executeQuery(sql2); 
                        String code, title = ""; 

                        while (rs.next()) { 
                                code = rs.getString("code"); 
                                title = rs.getString("title"); 
       
                                TreeModel model = new TreeModel(); 
                                model.setCode(code); 
                                model.setTitle(title); 

                                model.setUrl(
                                 "m/content/" + code + ".html"); 
                                model.setLeaf(false); 
                                model.setLevel(
                                code != null ? code.length() / 3 : 0); 
                                list.add(model); 
                        } 

                } catch (SQLException e) { 
                        e.printStackTrace(); 
                } finally { 
                        // DataSourceUtils.close(rs); 
                        // DataSourceUtils.close(st); 
                        DataSourceUtils.close(conn); 
                } 
                return list; 
        } 
}

生成js文件

package com.jxlt.db.parse; 

import org.apache.log4j.Logger; 

import java.util.List; 

/** 
* Generate the left tree menu 
* 
* @author martin.xus (martin.xus@gmail.com) 
*/ 
public class GeneratorTree { 
        private static Logger logger = Logger.getLogger(GeneratorTree.class); 

        public static final String TREE_FILE = "D:\workspace\style\test\martin.tree.002.js"; 

        /** 
        * Generate the left tree menu 
        * 
        * @return String 
        */ 
        public static String generator() { 
                StringBuffer buf = new StringBuffer().append(HEADER); 
                List list = ParseTree.parse(); 
                logger.debug("generating tree menu begin"); 
                TreeModel model; 
                TreeModel modelNext; 
                int position; 
                if (null != list && 0 < list.size()) { 
                        for (int i = 0, length = list.size(); i < length - 1; i++) { 
                                logger.debug("dealed with :" + i + " rows"); 
                                StringBuffer _buf = new StringBuffer(); 
                                model = (TreeModel) list.get(i); 
                                modelNext = (TreeModel) list.get(i + 1); 
                                _buf.append("['").append(model.getTitle()).append("','") 
                                                .append(model.getUrl()).append("'"); 

                                if (model.getLevel() > modelNext.getLevel()) { 
                                        _buf.append(SUFFIX).append(COMMA); 
                                } else if (model.getLevel() == modelNext.getLevel()) { 
                                        _buf.append(SUFFIX).append(COMMA).append(PLACEKICK); 
                                } else if (model.getLevel() < modelNext.getLevel()) { 
                                        _buf.append(COMMA).append(PLACEKICK).append(SUFFIX); 
                                } 

                                position = buf.toString().indexOf(PLACEKICK); 
                                if (position != -1) { 
                                        buf.replace(position, position + 7, _buf.toString()); 
                                } else { 
                                        buf.insert(buf.length() - model.getLevel() - 4, "," 
                                                        + _buf.toString()); 
                                } 

                                if (i == length - 2) { 
                                        _buf.append("['").append(modelNext.getTitle()) 
                                                        .append("','").append(modelNext.getUrl()).append( 
                                                                        "'").append(SUFFIX).append(COMMA); 
                                } 
                        } 
                } 
                return buf.toString(); 
        } 

        private static final String SUFFIX = "]"; 

        private static final String COMMA = ","; 

        private static final String PLACEKICK = "$martin"; 

        private static final String HEADER = "var TREE_ITEMS = [['索引','#'," 
                        + PLACEKICK + "]];"; 
}

test 一下:

logger.debug("writing file:" +
                   GeneratorTree.TREE_FILE); 
FileWriter writer = 
                   new FileWriter(GeneratorTree.TREE_FILE); 
writer.write(GeneratorTree.generator()); 
writer.flush(); 
writer.close(); 
logger.debug("end");

菜单js样本
一:

var TREE_ITEMS = [
	['index', '#',
		['001', '#001',
			
			['001001', '#001001',
				['001001001', '#001001001'],
			],
			
			['001002', '#001002',
				['001002001', '#001002001',
					['001002002', '#001002002'],
				],
			],
			
			['001003', '#001001003',
				['001003001', '#001003001',
					['001003001001', '#001003001001'],
					['001003001002', '#001003001001',
						['001003001002001', '#001003001002001'],					
					],
				],
			],
		],
		['002', '#002'],
	]
];
分享到:
评论