Hopes

          Start Here..

           

          使用xloadtree讀取數(shù)據(jù)庫動態(tài)構(gòu)造樹

           

          使用xloadtree讀取數(shù)據(jù)庫動態(tài)構(gòu)造樹

          分類: struts2008-11-01 00:19 1803人閱讀 評論(4) 收藏 舉報(bào)

          原先系統(tǒng)使用xtree讀取數(shù)據(jù)庫構(gòu)造的樹,是jsp一次性生成所有的節(jié)點(diǎn)的,因?yàn)閿?shù)據(jù)量也不大,所以用用還挺好。今天特來興致,決定改造。當(dāng)點(diǎn)擊父節(jié)點(diǎn)的時(shí)候,才去數(shù)據(jù)庫動態(tài)的獲取子節(jié)點(diǎn),這樣對于大數(shù)據(jù)量的系統(tǒng),性能比較好。

          首先下載 http://webfx.eae.net/dhtml/xtree2b/xtree2b-20050606.zip

          這包里面有簡單的示例,廢話少說,動手

          1. <link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/xtree2.css">
          2. <link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/xtree2.links.css">
          3. <script type="text/javascript" src="<%=request.getContextPath() %>/script/xtree2.js"></script>
          4. <script type="text/javascript" src="<%=request.getContextPath() %>/script/xloadtree2.js"></script>

          首先構(gòu)造樹的jsp文件要加載相關(guān)js和css文件,上面down的zip里面都有,放到你的項(xiàng)目里面

          jsp端加入腳本,構(gòu)造樹

          1. <div style="left:20px;top:30px;width:100%;">
          2. <script>
          3.     var tree = new WebFXTree('root node');
          4.     tree.add(new WebFXLoadTreeItem("all node", "/cmsCatalogQueryAction.do?method=tree","javascript:modifyNode('cmsCatalogMaintAction.do?method=new')"));
          5.     tree.showRootNode = false;
          6.     tree.showRootLines = false;
          7.     tree.write();
          8. </script>

          /cmsCatalogQueryAction.do?method=tree是生成相關(guān)xml的action地址

          javascript:modifyNode('cmsCatalogMaintAction.do?method=new')這個(gè)是點(diǎn)擊根節(jié)點(diǎn)的時(shí)候action的地址,比如鏈接到添加一級節(jié)點(diǎn)的頁面,(modifyNode是我項(xiàng)目里面用的js函數(shù),目的是在右邊的frame里面打開后面的參數(shù)地址)

              tree.showRootNode = false;
              tree.showRootLines = false;
          為了把a(bǔ)ll node作為根節(jié)點(diǎn),所以隱藏root node節(jié)點(diǎn)

           

          下面看看action端的寫法

          1. public ActionForward tree(
          2.             ActionMapping actionMapping,
          3.             ActionForm actionForm,
          4.             HttpServletRequest request,
          5.             HttpServletResponse response)
          6.             throws Exception {
          7.             response.setContentType("text/xml; charset=gbk");
          8.             response.setHeader("Cache-Control", "no-cache");
          9.             response.setDateHeader("Expires", 0);
          10.             PrintWriter out = response.getWriter();
          11.             String id = request.getParameter("id");
          12.             Document doc = new Document(new Element("tree"));
          13.             if (StringUtils.isEmpty(id)) {
          14.                 List list = getCmsFacade().getRootCatalogListByDomainID(HConstant.currentDomain.getId());
          15.                 transfer(doc, list);
          16.            } else {
          17.                 List list = getCmsFacade().getCmsCatalogListByParentID(id);
          18.                 transfer(doc, list);
          19.            }
          20.             Format format = Format.getCompactFormat();
          21.             format.setEncoding("gbk");
          22.             format.setIndent("/t");
          23.             XMLOutputter xout = new XMLOutputter(format);
          24.             xout.output(doc, out);
          25.             out.flush();
          26.             out.close();
          27.             return null;
          28.         } // end tree
          29. private void transfer(Document doc, List r) {
          30.         for (Iterator it = r.iterator(); it.hasNext();) {
          31.          CmsCatalog one = (CmsCatalog) it.next();
          32.          Element tree = doc.getRootElement();
          33.          Element inner = new Element("tree").setAttribute("text", one.getZhCaption());
          34.          tree.addContent(inner);
          35.          inner.setAttribute("src", "/cmsCatalogQueryAction.do?method=tree&id="+ one.getId());
          36.          inner.setAttribute("action", "javascript:refun('"+one.getId()+"','"+one.getZhCaption()+"')");
          37.          }
          38.        }

          這邊的代碼要根據(jù)你的業(yè)務(wù)來寫了,我的欄目對象叫CmsCatalog。生成xml使用了jdom,代碼中的Document 和Element這些class都是這個(gè)包里面的

          getCmsFacade().getRootCatalogListByDomainID是獲取本站點(diǎn)的所有一級欄目,

          getCmsFacade().getCmsCatalogListByParentID是根據(jù)父ID得到下一級欄目列表,(不是得到所有的子,只獲取直接下級的子)

           

          獲取到欄目list后,要轉(zhuǎn)換成對應(yīng)xml規(guī)范,參看transfer方法,轉(zhuǎn)后的數(shù)據(jù)格式如下,以根節(jié)點(diǎn)為例

          1. <?xml version="1.0" encoding="gbk" ?> 
          2. - <tree> 
          3. <tree text="關(guān)于我們" src="/cmsCatalogQueryAction.do?method=tree&id=4028e44919c7e5290119c7e754330001" action="javascript:refun('4028e44919c7e5290119c7e754330001','關(guān)于我們')" /> 
          4. <tree text="新聞頻道" src="/cmsCatalogQueryAction.do?method=tree&id=4028e4491a04a4fb011a04ab42ae005e" action="javascript:refun('4028e4491a04a4fb011a04ab42ae005e','新聞頻道')" /> 
          5. <tree text="產(chǎn)品與解決方案" src="/cmsCatalogQueryAction.do?method=tree&id=4028e4491a80ec5b011a811c1b570002" action="javascript:refun('4028e4491a80ec5b011a811c1b570002','產(chǎn)品與解決方案')" /> 
          6. <tree text="服務(wù)與支持" src="/cmsCatalogQueryAction.do?method=tree&id=4028e4491a900271011a9042bb810003" action="javascript:refun('4028e4491a900271011a9042bb810003','服務(wù)與支持')" /> 
          7. <tree text="英才導(dǎo)航" src="/cmsCatalogQueryAction.do?method=tree&id=4028e4491a33c105011a33d2f5b90002" action="javascript:refun('4028e4491a33c105011a33d2f5b90002','英才導(dǎo)航')" /> 
          8. <tree text="聯(lián)系我們" src="/cmsCatalogQueryAction.do?method=tree&id=4028e4491a805657011a80d2cae90006" action="javascript:refun('4028e4491a805657011a80d2cae90006','聯(lián)系我們')" /> 
          9. </tree>

          當(dāng)點(diǎn)擊一個(gè)節(jié)點(diǎn)的時(shí)候,會執(zhí)行refun這個(gè)js函數(shù),為什么不直接寫一個(gè)鏈接呢? 因?yàn)橐粋€(gè)欄目樹可能在很多地方都要用到,點(diǎn)擊樹的鏈接也因此可能不一樣了,所以不能在action寫死具體地址。構(gòu)造樹的jsp頁面直接寫一個(gè)refun函數(shù)就ok了,自己去定義點(diǎn)擊節(jié)點(diǎn)時(shí)候的操作。服務(wù)端返回了兩個(gè)欄目參數(shù)給refun函數(shù),一個(gè)是id,一個(gè)中文名稱

           

          當(dāng)點(diǎn)擊最后一級節(jié)點(diǎn)的時(shí)候,會提示加載節(jié)點(diǎn)錯(cuò)誤的信息,這個(gè)是xloadtree的一個(gè)bug,我們手動修改下就ok了,編輯xloadtree2.js文件,找到
          if (count == 0) {
                      jsNode.errorText = "Error loading " + jsNode.src + " (???)";
                  }

          修改為
          if (count == 0) {
                                  jsNode.remove(jsNode._loadingItem);
                                  t.setSuspendRedraw(false);
                                  jsNode.update();
                                  return;
            }
          當(dāng)返回的xml 為空的時(shí)候,把節(jié)點(diǎn)設(shè)置為無子節(jié)點(diǎn)

           

          到此,動態(tài)加載樹就完成了,相當(dāng)簡單吧,你唯一要做的就是合理的把你的欄目列表從數(shù)據(jù)庫取出來,然后正確的填充到xml里面,就ok了

          posted on 2012-08-12 21:39 ** 閱讀(1065) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          收藏夾

          C#學(xué)習(xí)

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 锡林郭勒盟| 通道| 马龙县| 柘城县| 南和县| 汉中市| 荆州市| 云安县| 辛集市| 涿州市| 上思县| 塔城市| 准格尔旗| 桂林市| 澎湖县| 山西省| 慈溪市| 焉耆| 华坪县| 仲巴县| 永福县| 芦溪县| 北京市| 喜德县| 罗山县| 石景山区| 佛坪县| 芦溪县| 子长县| 博白县| 西藏| 平南县| 西和县| 云梦县| 沽源县| 博罗县| 德令哈市| 武强县| 当阳市| 廉江市| 红河县|