开花流水

空山无人,水流花开。

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 开花流水 阅读(1272) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: