開花流水

          空山無人,水流花開。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            79 Posts :: 42 Stories :: 160 Comments :: 0 Trackbacks

          Entity部分關鍵代碼:

          Java代碼 
          1. public class PartType {  
          2.   
          3.     //屬性...  
          4.   
          5.     private PartType parent;  
          6.     private Set<PartType> children = new HashSet<PartType>();  
          7.   
          8.   
          9.     //屬性對應的getter、setter......  
          10.   
          11.     @ManyToOne  
          12.     public PartType getParent() {  
          13.         return parent;  
          14.     }  
          15.   
          16.     public void setParent(PartType parent) {  
          17.         this.parent = parent;  
          18.     }  
          19.   
          20.     @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")  
          21.     public Set<PartType> getChildren() {  
          22.         return children;  
          23.     }  
          24.   
          25.     public void setChildren(Set<PartType> children) {  
          26.         this.children = children;  
          27.     }  
          28.       
          29.     public void addChildren(PartType pt) {  
          30.         this.children.add(pt);  
          31.     }  
          32.       
          33.           
          34. }  

           

          SessionBean代碼...... 

           

          Action部分關鍵代碼:

          Java代碼 
          1. @Name("ptAction")  
          2. @Scope(ScopeType.CONVERSATION)  
          3. public class PartTypeAction {  
          4.   
          5.     @Logger  
          6.     private Log log;  
          7.   
          8.     @In  
          9.     private IPartTypeManager ptManager;  
          10.   
          11.     @Out(required = false)  
          12.     private PartType selectpt;  
          13.   
          14.     public PartType getSelectpt() {  
          15.         return selectpt;  
          16.     }  
          17.   
          18.     public void setSelectpt(PartType selectpt) {  
          19.         this.selectpt = selectpt;  
          20.     }  
          21.   
          22.     /* 
          23.      * 遞歸節點樹 
          24.      */  
          25.     private TreeNode<PartType> addChild(TreeNode<PartType> curNode,  
          26.             PartType curPT) {  
          27.         curNode.setData(curPT);  
          28.         log.info("遍歷: " + curPT.getName());  
          29.         if (curPT.getChildren().size() > 0) {  
          30.             for (Iterator iterator = curPT.getChildren().iterator(); iterator  
          31.                     .hasNext();) {  
          32.                 PartType childptItem = (PartType) iterator.next();  
          33.                 TreeNode<PartType> childpt = new TreeNodeImpl<PartType>();  
          34.                 curNode.addChild(childptItem.getId(), childpt);// 子節點加到當前節點下  
          35.                 addChild(childpt, childptItem);  
          36.             }  
          37.         }  
          38.         return curNode;  
          39.     }  
          40.   
          41.     /* 
          42.      * 構建樹 
          43.      */  
          44.     @Begin(join = true)  
          45.     public TreeNode<PartType> getPTTree() {  
          46.         log.info("構建PartType Tree");  
          47.         PartType ptroot = ptManager.getRootPartType();//調用SessionBean中的方法以獲取父節點  
          48.   
          49.         TreeNode<PartType> root = new TreeNodeImpl<PartType>();  
          50.         this.addChild(root, ptroot);  
          51.         TreeNode<PartType> vroot = new TreeNodeImpl<PartType>();  
          52.         vroot.addChild(root.getData().getId(), root);  
          53.         return vroot;  
          54.   
          55.     }  
          56.   
          57.     /* 
          58.      * 選擇一個節點觸發事件 
          59.      */  
          60.     @Begin(join = true)  
          61.     public void processSelection(NodeSelectedEvent event) {  
          62.         UITree tree = getTree(event);  
          63.         if (tree != null) {  
          64.             selectpt = (PartType) tree.getTreeNode().getData();  
          65.             log.info("選中節點:" + selectpt.getName());  
          66.             this.setSelectpt(selectpt);  
          67.         }  
          68.     }  
          69.   
          70.     private UITree getTree(FacesEvent event) {  
          71.         UIComponent component = event.getComponent();  
          72.         if (component instanceof UITree) {  
          73.             return ((UITree) component);  
          74.         }  
          75.   
          76.         if (component instanceof UITreeNode) {  
          77.             return ((UITree) component.getParent());  
          78.         }  
          79.   
          80.         return null;  
          81.     }  
          82.   
          83. }  

           

          頁面代碼:

          Jsf代碼 
          1. <rich:tree swidth="100%" value="#{ptAction.PTTree}" var="item"  
          2.     switchType="ajax" reRender="userdata"  
          3.     nodeSelectListener="#{ptAction.processSelection}"  
          4.     ajaxSubmitSelection="true">  
          5.     <rich:treeNode>  
          6.         <h:outputText value="#{item.name}" />  
          7.     </rich:treeNode>  
          8. </rich:tree>  



                                                                                                                                                                                  ————引用自http://q-wong.javaeye.com/?show_full=true
          posted on 2009-06-06 18:02 開花流水 閱讀(1282) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 沁阳市| 喀什市| 吐鲁番市| 延安市| 田林县| 屏山县| 临沭县| 陵水| 淅川县| 喀喇| 桦甸市| 大埔县| 万州区| 揭东县| 叙永县| 普定县| 江北区| 武威市| 克拉玛依市| 张家港市| 达州市| 卫辉市| 新巴尔虎左旗| 永平县| 茶陵县| 长海县| 柳林县| 喜德县| 铜陵市| 门头沟区| 砚山县| 涡阳县| 陆河县| 张家口市| 南岸区| 志丹县| 金平| 聊城市| 电白县| 尖扎县| 左权县|