OA項目----實現樹狀結構

      樹狀結構在項目中很常用,最近小編在做項目中涉及到了樹狀結構,現在給大家分享一下。

      樹狀結構有兩個核心:

           1.遞歸實現樹狀結構, 因爲涉及到父節點和子節點,所以我們需要層層遞歸才能得到一個節點的子節點。在後臺通過遞歸可以實現樹狀結構並返回。

public class DepartmentUtils {
    /**
     * 便利部門樹,把所有的不嫩便利出來放在一個集合中返回,並且其中所有的部門的名稱都修改了,以表示層次
     * @param topList
     * retrun*/
    public static List<Department> getAllDepartments(List<Department> topList) {
        
        List<Department> list=new ArrayList<Department>();
        
        walkDepartmentTreeList(topList,"|-",list);
        return list;
    }
    
    //遍歷部門樹,把遍歷出的部門信息放到指定的集合中
    private static void walkDepartmentTreeList(Collection<Department> topList, String prefix, List<Department> list){
        
        for(Department top:topList){
            //頂點
            
            top.setName(prefix + top.getName());
            list.add(top);
            //子樹,使用全角的空格,因爲全角的空格相當於字符
            walkDepartmentTreeList(top.getChildren()," "+prefix,list);
        }
        
    }
} 

            2.filter攔截action,因爲在session中有子結點的時候,父節點的session生命週期已經結束,如果想獲得後臺傳過來的父節點,就需要攔截session,使父節點的聲明週期不會結束,這就需要通過攔截action實現了。

    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章